Skip to content

Instantly share code, notes, and snippets.

@smunaut
Last active January 26, 2021 21:13
Show Gist options
  • Save smunaut/44d28519c2130303cfd6960c72cef54d to your computer and use it in GitHub Desktop.
Save smunaut/44d28519c2130303cfd6960c72cef54d to your computer and use it in GitHub Desktop.
CodeRender

Render code to image

Place wkhtmltoimage static build in this directory

#!/usr/bin/env python3
import sys
from pygments import highlight
from pygments.lexers import get_lexer_for_filename
from pygments.formatters import HtmlFormatter
HTML_TPL="""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
%(style)s
</style>
</head>
<body>
<div id="main" class="highlight">
%(render)s
</div>
</body>
</html>
"""
def main(argv0, in_filename, out_filename):
with open('style.css', 'r') as style_fh, open(in_filename, 'r') as in_fh, open(out_filename, 'w') as out_fh:
code = in_fh.read()
ts = 8
if in_filename.endswith('.v'):
ts = 4
code = code.replace('\t', ' '*ts)
lexer = get_lexer_for_filename(in_filename)
formatter = HtmlFormatter(linenos='table', cssclass="highlight")
result = highlight(code, lexer, formatter)
out_fh.write(HTML_TPL % {
'style': style_fh.read(),
'render': result
})
return 0
sys.exit(main(*sys.argv) or 0)
#!/bin/bash
set -e
WK_OPTS="--transparent --format png --quality 10"
./render.py $1 /tmp/render.html
./wkhtmltoimage ${WK_OPTS} /tmp/render.html $2
/* Style for render */
body {
background-color: black;
}
#main {
margin: auto;
width: 1280px;
border: 2px solid gray;
border-radius: 25px;
padding: 10px;
font-size: 16pt;
}
div.highlight {
width: 1230px;
}
pre {
overflow: hidden;
text-overflow: ellipsis;
}
td.linenos {
font-style: italic;
}
div.linenodiv pre {
color: gray !important;
}
/* From: https://github.com/StylishThemes/Syntax-Themes */
/*! Dark; http://bytefluent.com/devify/ */
/* Modified to highligh Punctuation */
.highlight, .highlight pre, .highlight table { background: #333333 !important; color: #f8f8ff !important; }
.highlight .hll { background-color: #ffffcc !important; }
.highlight .c { color: #87ceeb !important; } /* Comment */
.highlight .err { color: #f8f8ff !important; } /* Error */
.highlight .g { color: #f8f8ff !important; } /* Generic */
.highlight .k { color: #d2b48c !important; } /* Keyword */
.highlight .l { color: #f8f8ff !important; } /* Literal */
.highlight .n, .highlight .h { color: #f8f8ff !important; } /* Name */
.highlight .o { color: #f0f0a0 !important; } /* Operator */
.highlight .x { color: #f8f8ff !important; } /* Other */
.highlight .p { color: #f0f0a0 !important; } /* Punctuation */
.highlight .cm { color: #87ceeb !important; } /* Comment.Multiline */
.highlight .cp { color: #ee7ae9 !important; } /* Comment.Preproc */
.highlight .c1 { color: #87ceeb !important; } /* Comment.Single */
.highlight .cs { color: #87ceeb !important; } /* Comment.Special */
.highlight .gd { color: #f8f8ff !important; } /* Generic.Deleted */
.highlight .ge { color: #f8f8ff !important; } /* Generic.Emph */
.highlight .gr { color: #f8f8ff !important; background-color: #cd0000 !important; } /* Generic.Error */
.highlight .gh { color: #f8f8ff !important; } /* Generic.Heading */
.highlight .gi { color: #f8f8ff !important; } /* Generic.Inserted */
.highlight .go { color: #cdcd00 !important; background-color: #262626 !important; } /* Generic.Output */
.highlight .gp { color: #f8f8ff !important; } /* Generic.Prompt */
.highlight .gs { color: #f8f8ff !important; } /* Generic.Strong */
.highlight .gu { color: #f8f8ff !important; } /* Generic.Subheading */
.highlight .gt { color: #f8f8ff !important; } /* Generic.Traceback */
.highlight .kc { color: #d2b48c !important; } /* Keyword.Constant */
.highlight .kd { color: #d2b48c !important; } /* Keyword.Declaration */
.highlight .kn { color: #d2b48c !important; } /* Keyword.Namespace */
.highlight .kp { color: #d2b48c !important; } /* Keyword.Pseudo */
.highlight .kr { color: #d2b48c !important; } /* Keyword.Reserved */
.highlight .kt { color: #ffa500 !important; } /* Keyword.Type */
.highlight .ld { color: #f8f8ff !important; } /* Literal.Date */
.highlight .m { color: #FF7070 !important; } /* Literal.Number */
.highlight .s { color: #ffa0a0 !important; } /* Literal.String */
.highlight .na { color: #60DD60 !important; } /* Name.Attribute */
.highlight .nb { color: #f8f8ff !important; } /* Name.Builtin */
.highlight .nc { color: #f8f8ff !important; } /* Name.Class */
.highlight .no { color: #FF7070 !important; } /* Name.Constant */
.highlight .nd { color: #f8f8ff !important; } /* Name.Decorator */
.highlight .ni { color: #DDDD00 !important; } /* Name.Entity */
.highlight .ne { color: #ffa500 !important; } /* Name.Exception */
.highlight .nf { color: #60DD60 !important; } /* Name.Function */
.highlight .nl { color: #ffa500 !important; } /* Name.Label */
.highlight .nn { color: #f8f8ff !important; } /* Name.Namespace */
.highlight .nx { color: #f8f8ff !important; } /* Name.Other */
.highlight .py { color: #f8f8ff !important; } /* Name.Property */
.highlight .nt { color: #d2b48c !important; } /* Name.Tag */
.highlight .nv { color: #60DD60 !important; } /* Name.Variable */
.highlight .ow { color: #ffa500 !important; } /* Operator.Word */
.highlight .w { color: #f8f8ff !important; } /* Text.Whitespace */
.highlight .mf { color: #FF7070 !important; } /* Literal.Number.Float */
.highlight .mh { color: #FF7070 !important; } /* Literal.Number.Hex */
.highlight .mi { color: #FF7070 !important; } /* Literal.Number.Integer */
.highlight .mo { color: #FF7070 !important; } /* Literal.Number.Oct */
.highlight .sb { color: #ffa0a0 !important; } /* Literal.String.Backtick */
.highlight .sc { color: #ffa0a0 !important; } /* Literal.String.Char */
.highlight .sd { color: #ffa0a0 !important; } /* Literal.String.Doc */
.highlight .s2 { color: #ffa0a0 !important; } /* Literal.String.Double */
.highlight .se { color: #ffa0a0 !important; } /* Literal.String.Escape */
.highlight .sh { color: #ffa0a0 !important; } /* Literal.String.Heredoc */
.highlight .si { color: #ffa0a0 !important; } /* Literal.String.Interpol */
.highlight .sx { color: #ffa0a0 !important; } /* Literal.String.Other */
.highlight .sr { color: #ffa0a0 !important; } /* Literal.String.Regex */
.highlight .s1 { color: #ffa0a0 !important; } /* Literal.String.Single */
.highlight .ss { color: #ffa0a0 !important; } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8ff !important; } /* Name.Builtin.Pseudo */
.highlight .vc { color: #60DD60 !important; } /* Name.Variable.Class */
.highlight .vg { color: #60DD60 !important; } /* Name.Variable.Global */
.highlight .vi { color: #60DD60 !important; } /* Name.Variable.Instance */
.highlight .il { color: #FF7070 !important; } /* Literal.Number.Integer.Long */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment