From: Drew Fisher Date: Sun, 2 Oct 2022 03:20:14 +0000 (-0700) Subject: Update markdoku for python3 compat X-Git-Url: http://git.zarvox.org/shortlog/%7Bwebsite%7D?a=commitdiff_plain;h=ba5b9f708b644188880a9624fda680ceb22de7dc;p=shortlog.git Update markdoku for python3 compat --- diff --git a/markdoku.py b/markdoku.py index 3c87132..d4e3070 100644 --- a/markdoku.py +++ b/markdoku.py @@ -94,7 +94,7 @@ except ImportError: import optparse from random import random, randint import codecs -from urllib import quote +from urllib.parse import quote @@ -123,12 +123,12 @@ DEFAULT_TAB_WIDTH = 4 try: import uuid except ImportError: - SECRET_SALT = str(randint(0, 1000000)) + SECRET_SALT = str(randint(0, 1000000)).encode('utf-8') else: - SECRET_SALT = str(uuid.uuid4()) + SECRET_SALT = str(uuid.uuid4()).encode('utf-8') def _hash_ascii(s): #return md5(s).hexdigest() # Markdown.pl effectively does this. - return 'md5-' + md5(SECRET_SALT + s).hexdigest() + return 'md5-' + md5(SECRET_SALT + s.encode("utf-8")).hexdigest() def _hash_text(s): return 'md5-' + md5(SECRET_SALT + s.encode("utf-8")).hexdigest() @@ -253,9 +253,9 @@ class Markdown(object): # articles): self.reset() - if not isinstance(text, unicode): + if not isinstance(text, str): #TODO: perhaps shouldn't presume UTF-8 for string input? - text = unicode(text, 'utf-8') + text = text.decode('utf-8') if self.use_file_vars: # Look for emacs-style file variable hints. @@ -578,11 +578,11 @@ class Markdown(object): # Delimiters for next comment block. try: start_idx = text.index("", start_idx) + 3 - except ValueError, ex: + except ValueError as ex: break # Start position for next comment block search. @@ -1810,7 +1810,7 @@ class MarkdownWithExtras(Markdown): #---- internal support functions -class UnicodeWithAttrs(unicode): +class UnicodeWithAttrs(str): """A subclass of unicode used for the return value of conversion to possibly attach some attributes. E.g. the "toc_html" attribute when the "toc" extra is used. @@ -1921,8 +1921,7 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False): """ DEBUG = False if DEBUG: - print "dedent: dedent(..., tabsize=%d, skip_first_line=%r)"\ - % (tabsize, skip_first_line) + print(f"dedent: dedent(..., tabsize={tabsize}, skip_first_line={skip_first_line})") indents = [] margin = None for i, line in enumerate(lines): @@ -1939,12 +1938,12 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False): break else: continue # skip all-whitespace lines - if DEBUG: print "dedent: indent=%d: %r" % (indent, line) + if DEBUG: print(f"dedent: indent={indent}: {line}") if margin is None: margin = indent else: margin = min(margin, indent) - if DEBUG: print "dedent: margin=%r" % margin + if DEBUG: print(f"dedent: margin={margin}") if margin is not None and margin > 0: for i, line in enumerate(lines): @@ -1956,7 +1955,7 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False): elif ch == '\t': removed += tabsize - (removed % tabsize) elif ch in '\r\n': - if DEBUG: print "dedent: %r: EOL -> strip up to EOL" % line + if DEBUG: print("dedent: {line}: EOL -> strip up to EOL") lines[i] = lines[i][j:] break else: @@ -1964,8 +1963,7 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False): "line %r while removing %d-space margin" % (ch, line, margin)) if DEBUG: - print "dedent: %r: %r -> removed %d/%d"\ - % (line, ch, removed, margin) + print(f"dedent: {line}: {ch} -> removed {removed}/{margin}") if removed == margin: lines[i] = lines[i][j+1:] break @@ -2185,13 +2183,13 @@ def main(argv=None): "Markdown.pl") for path in paths: if opts.compare: - print "==== Markdown.pl ====" + print("==== Markdown.pl ====") perl_cmd = 'perl %s "%s"' % (markdown_pl, path) o = os.popen(perl_cmd) perl_html = o.read() o.close() sys.stdout.write(perl_html) - print "==== markdown2.py ====" + print("==== markdown2.py ====") html = markdown_path(path, encoding=opts.encoding, html4tags=opts.html4tags, safe_mode=opts.safe_mode, @@ -2212,7 +2210,8 @@ def main(argv=None): else: norm_html = html norm_perl_html = perl_html - print "==== match? %r ====" % (norm_perl_html == norm_html) + did_match = (norm_perl_html == norm_html) + print(f"==== match? {did_match} ====") if __name__ == "__main__":