From 51f8afa6a553460733ec1ef10ef792fe7ac9bb6a Mon Sep 17 00:00:00 2001 From: inaimathi Date: Thu, 10 Apr 2025 16:48:46 -0400 Subject: [PATCH 1/7] Indent with black --- pyhiccup/core.py | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/pyhiccup/core.py b/pyhiccup/core.py index abd37a1..92771b8 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,9 +71,9 @@ def _convert_tree(node): return btype = node[0] rest = node[1:] if len(node) > 1 else [] - attrs = '' + attrs = "" inner_trees = [] - inner_element = '' + inner_element = "" for element in rest: if not element: continue @@ -87,7 +87,7 @@ def _convert_tree(node): else: inner_element = element if inner_element or inner_trees: - yield '<%s%s>' % ( + yield "<%s%s>" % ( btype, attrs, ) @@ -96,9 +96,9 @@ def _convert_tree(node): for ext in inner_trees: for x in _convert_tree(ext): yield x - yield '' % btype + yield "" % btype else: - yield '<%s%s/>' % ( + yield "<%s%s/>" % ( btype, attrs, ) @@ -117,19 +117,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 @@ -146,7 +140,7 @@ def html(value, etype='html5', **kwargs): declaration = get_doc_type(etype) enclosing_tag = build_html_enclosing_tag(etype) converted = _inclose_page(declaration, enclosing_tag, value) - return ''.join(converted) + return "".join(converted) def xml(value, etype, **kwargs): @@ -165,7 +159,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 +171,4 @@ def convert(value): :return: XML string representation :rtype: str, unicode """ - return ''.join(_convert_tree(value)) + return "".join(_convert_tree(value)) From faa4acd0091b2bade177383faa8ce829a983c0b1 Mon Sep 17 00:00:00 2001 From: inaimathi Date: Thu, 10 Apr 2025 16:49:22 -0400 Subject: [PATCH 2/7] Actually pass kwargs to html_enclosing_tag to allow override of dir --- pyhiccup/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyhiccup/core.py b/pyhiccup/core.py index 92771b8..fb6762b 100644 --- a/pyhiccup/core.py +++ b/pyhiccup/core.py @@ -138,7 +138,7 @@ 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) From 5baf43bfbf0e974debd94cf53c44828745e82c37 Mon Sep 17 00:00:00 2001 From: inaimathi Date: Thu, 10 Apr 2025 16:49:46 -0400 Subject: [PATCH 3/7] Bump version --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 140e3c9ef114d4ce3d4953645fadbdc676998e4c Mon Sep 17 00:00:00 2001 From: inaimathi Date: Thu, 10 Apr 2025 16:50:22 -0400 Subject: [PATCH 4/7] Add emacs tempfiles to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) 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 From 4226f769dbf05da49aa6afa03c46017e16018056 Mon Sep 17 00:00:00 2001 From: inaimathi Date: Thu, 10 Apr 2025 17:03:50 -0400 Subject: [PATCH 5/7] Indent page.py with black --- pyhiccup/page.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/pyhiccup/page.py b/pyhiccup/page.py index 44e890d..b91ec0c 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"] = "rtl" + 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): From 80eaa92f995d7aa9ae268b8c58f49f88b2cdef66 Mon Sep 17 00:00:00 2001 From: inaimathi Date: Thu, 10 Apr 2025 17:04:09 -0400 Subject: [PATCH 6/7] Since lang defaults to en, default dir to ltr --- pyhiccup/page.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyhiccup/page.py b/pyhiccup/page.py index b91ec0c..5121ee9 100644 --- a/pyhiccup/page.py +++ b/pyhiccup/page.py @@ -67,7 +67,7 @@ def build_html_enclosing_tag(etype, **kwargs): attrs = {} if etype in DOC_TYPES: attrs["lang"] = "en" - attrs["dir"] = "rtl" + attrs["dir"] = "ltr" attrs["xml:lang"] = "en" if "xhtml" in etype: attrs["xmlns"] = DEFAULT_XMLNS From e67b1191ce102a1c8318067e4ed959b94309f9a2 Mon Sep 17 00:00:00 2001 From: inaimathi Date: Thu, 10 Apr 2025 18:39:06 -0400 Subject: [PATCH 7/7] Fix string child element ordering bug --- pyhiccup/core.py | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/pyhiccup/core.py b/pyhiccup/core.py index fb6762b..6338be0 100644 --- a/pyhiccup/core.py +++ b/pyhiccup/core.py @@ -72,30 +72,22 @@ def _convert_tree(node): 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: + 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 + 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/>" % (