[PATCH] Correct md2html's output order

Daniel Campbell dlcampbell at gmx.com
Sat May 21 12:25:44 CEST 2016


Corrects md2html's output order by setting two Python-centric
environment variables before calling it. This ensures two things:

* The io streams are UTF-8, which Python 3 handles natively
* Unbuffered output will create the expected behavior by flushing on
  print() instead of embedding the expected HTML in the <style>
  element due to Python's automatic buffering.
---
 filters/about-formatting.sh     | 7 ++++++-
 filters/html-converters/md2html | 4 ++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/filters/about-formatting.sh b/filters/about-formatting.sh
index 85daf9c..7e09be6 100755
--- a/filters/about-formatting.sh
+++ b/filters/about-formatting.sh
@@ -19,7 +19,12 @@
 
 cd "$(dirname $0)/html-converters/"
 case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
-	*.markdown|*.mdown|*.md|*.mkd) exec ./md2html; ;;
+	*.markdown|*.mdown|*.md|*.mkd)
+		# Ensure Python is using a UTF-8 stream
+		export PYTHONIOENCODING="utf-8"
+		# Force Python to flush buffers when printing to stdout
+		export PYTHONUNBUFFERED="true"
+		exec ./md2html; ;;
 	*.rst) exec ./rst2html; ;;
 	*.[1-9]) exec ./man2html; ;;
 	*.htm|*.html) exec cat; ;;
diff --git a/filters/html-converters/md2html b/filters/html-converters/md2html
index c8ee7d9..9911523 100755
--- a/filters/html-converters/md2html
+++ b/filters/html-converters/md2html
@@ -281,9 +281,9 @@ print('''
 ''')
 print(HtmlFormatter(style='pastie').get_style_defs('.highlight'))
 print('''
-</style>   
+</style>
+<div class="markdown-body">
 ''')
-print("<div class='markdown-body'>")
 # Note: you may want to run this through bleach for sanitization
 markdown.markdownFromFile(output_format="html5", extensions=["markdown.extensions.fenced_code", "markdown.extensions.codehilite", "markdown.extensions.tables"], extension_configs={"markdown.extensions.codehilite":{"css_class":"highlight"}})
 print("</div>")
-- 
2.8.2



More information about the CGit mailing list