Files
zulip/tools/setup/build_pygments_data.py
Cory Lynch 0965c43238 Add typeahead for syntax highlighting languages.
Modified composebox_typeahead.js to recognize the triple backtick
and tilde for code blocks, and added appropriate typeahead functions
in that file and in typeahead_helper.js.

Additionally, a new file pygments_data.js contains a dictionary of
the supported languages, mapping to relative popularity
rankings. These rankings determine the order of sort of the
languages in the typeahead.

This JavaScript file is actually in static/generated/pygments_data.js, as it
is generated by a Python script, tools/build_pymgents_data.py. This is
so that if Pygments adds support for new languages, the JavaScript file
will be updated appropriately. This python script uses a set of popularity
rankings defined in lang.json.

Corresponding unit tests were also added.

Fixes #4111.
2017-04-28 17:22:59 -07:00

35 lines
765 B
Python
Executable File

#!/usr/bin/env python
from pygments.lexers import get_all_lexers
import json
import os
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../')
DATA_PATH = os.path.join(ZULIP_PATH, 'tools', 'setup', 'lang.json')
JS_PATH = os.path.join(ZULIP_PATH, 'static', 'generated', 'pygments_data.js')
with open(DATA_PATH) as f:
langs = json.load(f)
lexers = get_all_lexers()
for lexer in lexers:
for name in lexer[1]:
if name not in langs:
langs[name] = 0
template = '''var pygments_data = (function () {
var exports = {};
exports.langs = %s;
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = pygments_data;
}''' % json.dumps(langs)
with open(JS_PATH, 'w') as f:
f.write(template)