Skip to content

Instantly share code, notes, and snippets.

@lkarthee
Last active April 15, 2021 17:14
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 lkarthee/31711aecf0158463152d368d43fe6ba7 to your computer and use it in GitHub Desktop.
Save lkarthee/31711aecf0158463152d368d43fe6ba7 to your computer and use it in GitHub Desktop.
Static Code Formatting for Dragon Ruby Docs

Introduction

This script adds syntax highlighting to code sections in DragonRuby docs. No js required.

You don't need to change any dragon ruby files to transform docs.html. It will generate output.html and appends css to docs.css

Dependencies

  1. Python 3
  2. pip install beautifulsoup4
  3. pip install Pygments

Run

Goto docs folder in Dragon Ruby installation. Here you should find docs.html. Copy dragon_pyg.py to same folder.

Run the following command in your shell after navigating to docs folder python3 dragon_pyg.py

It will take some time to generate the output.html. If you like it rename it docs.html

Note: original docs.html is around 1.5 mb. This generates 6 mb html file.

from bs4 import BeautifulSoup
from pygments import highlight
from pygments.lexers import RubyLexer
from pygments.formatters import HtmlFormatter
if __name__ == "__main__":
with open("pyg.html") as fp:
soup = BeautifulSoup(fp, 'html.parser')
code_tags = soup.find_all('code', class_="language-ruby")
for code_tag in code_tags:
if code_tag.string is not None:
text = highlight(code_tag.string, RubyLexer(), HtmlFormatter())
new_tag = BeautifulSoup(text, 'html.parser')
parent = code_tag.parent
code_tag.decompose()
parent.replace_with(new_tag)
with open("docs.css", "a", encoding='utf-8') as css_file:
print("appending to docs.css")
css_file.write(HtmlFormatter().get_style_defs('.highlight'))
css_file.close()
with open("output.html", "w", encoding='utf-8') as file:
print("writing to output.html")
file.write(str(soup))
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment