diff --git a/.gitignore b/.gitignore index db4561e..4e322c9 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,6 @@ docs/_build/ # PyBuilder target/ + +# Emacs +*~ \ No newline at end of file diff --git a/pyhiccup/core.py b/pyhiccup/core.py index abd37a1..6338be0 100644 --- a/pyhiccup/core.py +++ b/pyhiccup/core.py @@ -20,15 +20,15 @@ # ############################################################################## from __future__ import unicode_literals -import logging + import copy +import logging from itertools import chain -from .page import get_doc_type -from .page import build_html_enclosing_tag, build_xml_enclosing_tag -from .page import XMl_DECLARATION +from .page import (XMl_DECLARATION, build_html_enclosing_tag, + build_xml_enclosing_tag, get_doc_type) -_logger = logging.getLogger('pyhiccup.convert') +_logger = logging.getLogger("pyhiccup.convert") TREE_TYPE = (list, tuple) @@ -48,8 +48,8 @@ def format_attributes(attributes): """ output = [] for item in sorted(attributes.items()): - output.append('%s=\"%s\"' % item) - return " %s" % ' '.join(output) + output.append('%s="%s"' % item) + return " %s" % " ".join(output) def _convert_tree(node): @@ -63,7 +63,7 @@ def _convert_tree(node): :return: a list of string :rtype: list """ - #perfo tweak with side effect + # perfo tweak with side effect if isinstance(node[0], TREE_TYPE): for sub_node in node: for x in _convert_tree(sub_node): @@ -71,34 +71,26 @@ def _convert_tree(node): return btype = node[0] rest = node[1:] if len(node) > 1 else [] - attrs = '' - inner_trees = [] - inner_element = '' - for element in rest: - if not element: - continue - if isinstance(element, TREE_TYPE): - if isinstance(element[0], TREE_TYPE): - inner_trees.extend(element) - else: - inner_trees.append(element) - elif isinstance(element, dict): - attrs = format_attributes(element) - else: - inner_element = element - if inner_element or inner_trees: - yield '<%s%s>' % ( + attrs = "" + if rest and type(rest[0]) is dict: + attrs = format_attributes(rest[0]) + rest = rest[1:] + if rest: + yield "<%s%s>" % ( btype, attrs, ) - yield inner_element - if inner_trees: - for ext in inner_trees: - for x in _convert_tree(ext): - yield x - yield '' % btype + for element in rest: + if not element: + continue + elif isinstance(element, TREE_TYPE): + for el in _convert_tree(element): + yield el + else: + yield element + yield "" % btype else: - yield '<%s%s/>' % ( + yield "<%s%s/>" % ( btype, attrs, ) @@ -117,19 +109,13 @@ def _inclose_page(declaration, enclosing_tag, value): """ to_convert = copy.deepcopy(enclosing_tag) to_convert.append(value) - converted = chain( - [declaration], - _convert_tree(to_convert) - ) + converted = chain([declaration], _convert_tree(to_convert)) if _logger.getEffectiveLevel() == logging.DEBUG: - _logger.debug( - list(chain([declaration], - _convert_tree(to_convert))) - ) + _logger.debug(list(chain([declaration], _convert_tree(to_convert)))) return converted -def html(value, etype='html5', **kwargs): +def html(value, etype="html5", **kwargs): """Transform a list describing HTML to raw HTML :param value: list of list describing HTML @@ -144,9 +130,9 @@ def html(value, etype='html5', **kwargs): :rtype: str, unicode """ declaration = get_doc_type(etype) - enclosing_tag = build_html_enclosing_tag(etype) + enclosing_tag = build_html_enclosing_tag(etype, **kwargs) converted = _inclose_page(declaration, enclosing_tag, value) - return ''.join(converted) + return "".join(converted) def xml(value, etype, **kwargs): @@ -165,7 +151,8 @@ def xml(value, etype, **kwargs): declaration = XMl_DECLARATION enclosing_tag = build_xml_enclosing_tag(etype, **kwargs) converted = _inclose_page(declaration, enclosing_tag, value) - return ''.join(converted) + return "".join(converted) + def convert(value): """Transform a list to arbitratry XML @@ -176,4 +163,4 @@ def convert(value): :return: XML string representation :rtype: str, unicode """ - return ''.join(_convert_tree(value)) + return "".join(_convert_tree(value)) diff --git a/pyhiccup/page.py b/pyhiccup/page.py index 44e890d..5121ee9 100644 --- a/pyhiccup/page.py +++ b/pyhiccup/page.py @@ -22,19 +22,17 @@ from __future__ import unicode_literals DOC_TYPES = { - 'html4': "\n", - - 'xhtml-strict': "\n", - - 'xhtml-transitional': "\n", - - 'html5': "\n", + "html4": '\n', + "xhtml-strict": '\n', + "xhtml-transitional": '\n', + "html5": "\n", } -DEFAULT_XMLNS = 'http://www.w3.org/1999/xhtml' +DEFAULT_XMLNS = "http://www.w3.org/1999/xhtml" XMl_DECLARATION = '' @@ -49,8 +47,7 @@ def get_doc_type(doc_type): """ if doc_type not in DOC_TYPES: raise ValueError( - 'Invalid DOCTYPE %s available values are %s' % - (doc_type, DOC_TYPES.keys()) + "Invalid DOCTYPE %s available values are %s" % (doc_type, DOC_TYPES.keys()) ) return DOC_TYPES[doc_type] @@ -69,13 +66,13 @@ def build_html_enclosing_tag(etype, **kwargs): """ attrs = {} if etype in DOC_TYPES: - attrs['lang'] = 'en' - attrs['dir'] = 'rtl' - attrs['xml:lang'] = 'en' - if 'xhtml' in etype: - attrs[u'xmlns'] = DEFAULT_XMLNS + attrs["lang"] = "en" + attrs["dir"] = "ltr" + attrs["xml:lang"] = "en" + if "xhtml" in etype: + attrs["xmlns"] = DEFAULT_XMLNS attrs.update(kwargs) - return ['html', attrs] + return ["html", attrs] def build_xml_enclosing_tag(etype, **kwargs): diff --git a/setup.cfg b/setup.cfg index 924b8f9..dc145dd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,7 +8,7 @@ description-file = README.rst home-page = http://pypi.python.org/pypi/pyhiccup requires-python = >=2.6 classifier = -Development Status :: 0.1 +Development Status :: 0.2 Environment :: Console Intended Audience :: Developers Operating System :: OS Independent