Skip to content

Instantly share code, notes, and snippets.

Created April 5, 2012 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2310803 to your computer and use it in GitHub Desktop.
Save anonymous/2310803 to your computer and use it in GitHub Desktop.
HTML Navigation and Syntax Highlighting using Jython + Jsoup + Pygments
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