Skip to content

Commit

Permalink
Convert to code fences (#224)
Browse files Browse the repository at this point in the history
* Convert to code fences

* Improve handling of conde fences and nested lists
  • Loading branch information
pfultz2 committed Jan 12, 2023
1 parent 56626e0 commit 63ac019
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,53 @@ def parse_version():

nitpicky = True

def convert_to_code_fences(lines, fence='```'):
output = []
incode = False
isempty = False
infence = False
for line in lines:
sline = line.strip()
iscode = (isempty or incode) and line.startswith(' ') and not infence
isempty = not sline
isfence = line.startswith('```')
if infence:
if isfence:
infence = False
output.append(line)
elif incode:
if isempty:
output.append(line)
elif iscode:
output.append(line[4:])
else:
output.append(fence)
output.append('\n\n')
output.append(line)
incode = False
else:
if iscode:
nested_list = sline.startswith(('-', '*', '+')) or sline[0].isdigit()
x = line[4:]
if nested_list:
output.append(line)
elif x.startswith('!'):
output.append('{fence}{language}'.format(
fence=fence, language=x[1:].strip()))
output.append('\n')
else:
output.append(fence+'cpp')
output.append('\n')
output.append(x)
incode = not nested_list
elif isfence:
infence = True
incode = False
output.append(line)
else:
output.append(line)
return '\n'.join(output)

def insert_header(lines, f):
for line in lines:
yield line
Expand All @@ -428,6 +475,9 @@ def extract_doc(app, docname, source):
lines = source[0].split('\n')
md = [line[len(extract_prefix):] for line in lines if line.startswith(extract_prefix)]
source[0] = '\n'.join(insert_header(md, os.path.relpath(path, include_dir)))
if path.endswith(('.hpp', '.md')):
lines = source[0].split('\n')
source[0] = convert_to_code_fences(lines)

# app setup hook
def setup(app):
Expand Down

0 comments on commit 63ac019

Please sign in to comment.