Permalink
Browse files

Implement a basic Markdown to HTML converter.

Featuring pandoc and Pygments.
  • Loading branch information...
1 parent 1e702bd commit 4e780ee34f9dfe0d96077c142d49b925345ed8d0 @romac romac committed Nov 27, 2011
Showing with 104 additions and 0 deletions.
  1. +43 −0 Makefile
  2. +3 −0 _assets/footer.html
  3. +11 −0 _assets/header.html
  4. +14 −0 _assets/stylesheet.css
  5. +33 −0 _scripts/preprocess.py
View
@@ -0,0 +1,43 @@
+
+TEMP_DIR:=$(shell mktemp -d -t /tmp)
+PANDOC=pandoc
+BUILD_DIR=_build
+MD_OUTPUT=documentation.md
+HTML_OUTPUT=Documentation.html
+
+all: convert build
+ @echo '-- The documentation has been successfully generated.'
+
+build:
+ @echo -n '-- Copying the required assets into the build folder...'
+ @cp _assets/stylesheet.css $(BUILD_DIR)/
+ @cat $(TEMP_DIR)/highlight.css >> $(BUILD_DIR)/stylesheet.css
+ @cat _assets/header.html $(TEMP_DIR)/$(HTML_OUTPUT) _assets/footer.html > _build/index.html
+ @echo ' Done.'
+
+cprsrc:
+ @echo -n '-- Copying the required assets into the temp folder...'
+ @ditto 0*.md* $(TEMP_DIR)/
+ @echo ' Done.'
+
+convert: md2html
+
+concat: cprsrc
+ @echo -n '-- Concatenating the Markdown files...'
+ @cat $(TEMP_DIR)/0*.md > $(TEMP_DIR)/$(MD_OUTPUT)
+ @echo ' Done.'
+
+preprocess:
+ @echo -n '-- Syntax-highlighting code blocks...'
+ @python _scripts/preprocess.py $(TEMP_DIR)/$(MD_OUTPUT) $(TEMP_DIR)
+ @echo ' Done.'
+
+md2html: concat preprocess
+ @echo -n '-- Converting the Markdown document to HTML...'
+ @$(PANDOC) --html5 --toc -o $(TEMP_DIR)/$(HTML_OUTPUT) $(TEMP_DIR)/$(MD_OUTPUT)
+ @echo ' Done.'
+
+clean:
+ @rm -rf $(TEMP_DIR)
+
+.PHONY: all
View
@@ -0,0 +1,3 @@
+
+</body>
+</html>
View
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8" />
+ <title>the ooc language</title>
+ <link rel="stylesheet" href="stylesheet.css" type="text/css" />
+</head>
+<body>
+ <header>
+ <h1>the ooc language</h1>
+ </header>
View
@@ -0,0 +1,14 @@
+
+/* Stylesheet */
+
+body
+{
+ font-size: 16px;
+ font-family: Georgia, serif;
+ line-height: 1.6;
+}
+
+.highlight {
+ font-family: Monaco, Menlo, Droid Sans, monospace;
+ font-size: 0.9em;
+}
View
@@ -0,0 +1,33 @@
+
+import sys
+import re
+
+from pygments import highlight
+from pygments.formatters import HtmlFormatter
+from pygments.lexers import OocLexer
+
+pattern = re.compile(
+ r'(~{3,})(.+?)\1',
+ re.S
+)
+
+formatter = HtmlFormatter( style='trac' )
+lexer = OocLexer()
+
+sourceFile = open( sys.argv[ 1 ], 'r+' )
+source = sourceFile.read()
+
+matches = pattern.finditer( source )
+
+for match in matches:
+ source = source.replace( match.group(), highlight( match.group( 2 ), lexer, formatter ) )
+
+sourceFile.seek( 0 )
+sourceFile.write( source )
+sourceFile.close()
+
+css = formatter.get_style_defs( '.highlight' )
+cssFile = open( sys.argv[ 2 ] + '/highlight.css', 'w' )
+cssFile.write( '\n/* Pygments CSS for style "trac" */\n\n' )
+cssFile.write( css )
+cssFile.close()

0 comments on commit 4e780ee

Please sign in to comment.