Created
April 5, 2012 12:45
-
-
Save anonymous/2310803 to your computer and use it in GitHub Desktop.
HTML Navigation and Syntax Highlighting using Jython + Jsoup + Pygments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob, os, pygments, sys | |
sys.path.append(os.path.join(os.path.dirname(__file__), "jsoup.jar")) | |
import org.jsoup.Jsoup | |
formatter = pygments.formatters.HtmlFormatter(nowrap=True) | |
docs = [(org.jsoup.Jsoup.parse(open(f).read()), f) for f in glob.glob("*.html")] | |
titles = [(doc.title(), f) for doc, f in docs] | |
titles.sort() | |
for doc, f in docs: | |
ul = doc.select("nav ul").first().empty() | |
for text, href in titles: ul.appendElement("li").appendElement("a").attr("href", href).text(text) | |
for code in doc.select("code"): | |
if code.hasClass("bash"): code.html(pygments.highlight(code.text(), pygments.lexers.BashLexer(), formatter)) | |
elif code.hasClass("python"): code.html(pygments.highlight(code.text(), pygments.lexers.PythonLexer(), formatter)) | |
open(f, "w").write(doc.outerHtml()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment