M liquid_highlight/__main__.py +5 -1
@@ 1,6 1,10 @@
+import pathlib
import sys
from .highlight import highlight_all
def main():
- sys.stdout.write(highlight_all(sys.stdin.read()))
No newline at end of file
+ for filename in sys.argv[1:]:
+ sys.stdout.write(highlight_all(pathlib.Path(filename).open().read()))
+ if len(sys.argv) == 1:
+ sys.stdout.write(highlight_all(sys.stdin.read()))
No newline at end of file
M liquid_highlight/highlight.py +13 -1
@@ 1,3 1,4 @@
+import pathlib
import re
from pygments.lexers import get_lexer_by_name
@@ 9,15 10,26 @@ BLOCK = re.compile(
r'{% highlight (?P<language>.*?) %}\n(?P<code>.*?)\n{% endhighlight %}',
re.DOTALL
)
+CACHE_DIR = pathlib.Path('/tmp/pygments-cache/')
+CACHE_DIR.mkdir(exist_ok=True)
+
def highlight_block(match):
data = match.groupdict()
+ cache = CACHE_DIR / '{language}.{hash}.html'.format(hash=hash(data['code']), **data)
+
+ if cache.exists():
+ return cache.open().read()
+
formatter = HtmlFormatter(
noclasses=True,
linenos='linenos' in data
)
lexer = get_lexer_by_name(data['language'], stripall=True)
- return pygments.highlight(data['code'], lexer, formatter)
+ output = pygments.highlight(data['code'], lexer, formatter)
+ cache.open('w').write(output)
+ return output
+
def highlight_all(data):