mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
docs: Fix coding style of Python scripts using YAPF (#2606)
This commit is contained in:
@@ -8,13 +8,17 @@ import re
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
|
||||
# Prevent our markdown parser from trying to help by interpreting things in angle brackets as HTML tags.
|
||||
class EscapeHtml(Extension):
|
||||
|
||||
def extendMarkdown(self, md, md_globals):
|
||||
del md.preprocessors['html_block']
|
||||
del md.inlinePatterns['html']
|
||||
|
||||
|
||||
class RoffWalker(object):
|
||||
|
||||
def __init__(self, tree, output=sys.stdout):
|
||||
self.tree = tree
|
||||
self.target = output
|
||||
@@ -62,13 +66,16 @@ class RoffWalker(object):
|
||||
last_tag = None
|
||||
while root is not None:
|
||||
if root.tag == 'h1':
|
||||
self.__write_cmd('.TH "JQ" "1" "{}" "" ""'.format(date.today().strftime('%B %Y')))
|
||||
self.__write_cmd('.TH "JQ" "1" "{}" "" ""'.format(
|
||||
date.today().strftime('%B %Y')))
|
||||
self.__write_cmd('.SH "NAME"')
|
||||
# TODO: properly parse this
|
||||
self.__write_raw(r'\fBjq\fR \- Command\-line JSON processor' + "\n")
|
||||
self.__write_raw(r'\fBjq\fR \- Command\-line JSON processor' +
|
||||
"\n")
|
||||
|
||||
elif root.tag == 'h2':
|
||||
self.__write_cmd('.SH "{}"'.format(''.join(root.itertext()).strip()))
|
||||
self.__write_cmd('.SH "{}"'.format(''.join(
|
||||
root.itertext()).strip()))
|
||||
|
||||
elif root.tag == 'h3':
|
||||
text = ''.join(root.itertext()).strip()
|
||||
@@ -86,7 +93,8 @@ class RoffWalker(object):
|
||||
self._write_element(li)
|
||||
next = root.getnext()
|
||||
while next is not None and next.tag == 'p':
|
||||
if next.getnext() is not None and next.getnext().tag == 'pre':
|
||||
if next.getnext() is not None and next.getnext(
|
||||
).tag == 'pre':
|
||||
# we don't want to .IP these, because it'll look funny with the code indent
|
||||
break
|
||||
self.__write_cmd('.IP')
|
||||
@@ -185,14 +193,17 @@ class RoffWalker(object):
|
||||
print(dat, sep='', end='', file=self.f)
|
||||
pass
|
||||
|
||||
|
||||
def load_yml_file(fn):
|
||||
with open(fn) as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
|
||||
def dedent_body(body):
|
||||
lines = [re.sub(r'^ (\S)', r'\1', l) for l in body.split('\n')]
|
||||
return '\n'.join(lines)
|
||||
|
||||
|
||||
def convert_manual_to_markdown():
|
||||
f = StringIO()
|
||||
manual = load_yml_file("content/manual/manual.yml")
|
||||
@@ -222,11 +233,13 @@ def convert_manual_to_markdown():
|
||||
f.write(manual.get('manpage_epilogue', ''))
|
||||
return f.getvalue()
|
||||
|
||||
|
||||
# Convert manual.yml to our special markdown format
|
||||
markdown_data = convert_manual_to_markdown()
|
||||
|
||||
# Convert markdown to html
|
||||
html_data = markdown.markdown(markdown_data, extensions=[EscapeHtml(), 'fenced_code'])
|
||||
html_data = markdown.markdown(markdown_data,
|
||||
extensions=[EscapeHtml(), 'fenced_code'])
|
||||
|
||||
# Parse the html into a tree so we can walk it
|
||||
tr = etree.HTML(html_data, etree.HTMLParser())
|
||||
|
@@ -14,24 +14,26 @@ env = Environment(
|
||||
autoescape=select_autoescape(['html.j2']),
|
||||
)
|
||||
|
||||
|
||||
def load_yml_file(fn):
|
||||
with open(fn) as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
|
||||
env.filters['search_id'] = lambda input: input.replace(r'`', '')
|
||||
env.filters['section_id'] = lambda input: re.sub(r"[^a-zA-Z0-9_]", '', input)
|
||||
env.filters['entry_id'] = lambda input: re.sub(r"[ `]", '', input)
|
||||
env.filters['markdownify'] = lambda input: Markup(markdown(input))
|
||||
env.filters['no_paragraph'] = lambda input: Markup(re.sub(r"</?p>", '', input))
|
||||
|
||||
env.globals['unique_id'] = contextfunction(lambda ctx: str(next(ctx['unique_ctr'])))
|
||||
|
||||
env.globals['unique_id'] = contextfunction(
|
||||
lambda ctx: str(next(ctx['unique_ctr'])))
|
||||
|
||||
env.globals.update(load_yml_file('site.yml'))
|
||||
|
||||
|
||||
env.globals['navigation'] = ['tutorial', 'download', 'manual']
|
||||
|
||||
|
||||
def generate_file(env, fname='content/1.tutorial/default.yml'):
|
||||
path, base = os.path.split(fname)
|
||||
path = os.path.relpath(path, 'content')
|
||||
@@ -49,9 +51,13 @@ def generate_file(env, fname='content/1.tutorial/default.yml'):
|
||||
template_name = re.sub(r".yml$", '.html.j2', base)
|
||||
|
||||
ctx = load_yml_file(fname)
|
||||
ctx.update(unique_ctr=itertools.count(1), permalink=permalink, slug=slug, navitem=path)
|
||||
ctx.update(unique_ctr=itertools.count(1),
|
||||
permalink=permalink,
|
||||
slug=slug,
|
||||
navitem=path)
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
env.get_template(template_name).stream(ctx).dump(output_path, encoding='utf-8')
|
||||
env.get_template(template_name).stream(ctx).dump(output_path,
|
||||
encoding='utf-8')
|
||||
|
||||
|
||||
def copy_public_files(root=''):
|
||||
@@ -64,6 +70,7 @@ def copy_public_files(root=''):
|
||||
else:
|
||||
shutil.copyfile(f.path, dst)
|
||||
|
||||
|
||||
copy_public_files()
|
||||
|
||||
for fn in glob.glob('content/**/*.yml', recursive=True):
|
||||
|
Reference in New Issue
Block a user