Skip to content

Commit 63ac019

Browse files
authored
Convert to code fences (#224)
* Convert to code fences * Improve handling of conde fences and nested lists
1 parent 56626e0 commit 63ac019

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

doc/conf.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,53 @@ def parse_version():
407407

408408
nitpicky = True
409409

410+
def convert_to_code_fences(lines, fence='```'):
411+
output = []
412+
incode = False
413+
isempty = False
414+
infence = False
415+
for line in lines:
416+
sline = line.strip()
417+
iscode = (isempty or incode) and line.startswith(' ') and not infence
418+
isempty = not sline
419+
isfence = line.startswith('```')
420+
if infence:
421+
if isfence:
422+
infence = False
423+
output.append(line)
424+
elif incode:
425+
if isempty:
426+
output.append(line)
427+
elif iscode:
428+
output.append(line[4:])
429+
else:
430+
output.append(fence)
431+
output.append('\n\n')
432+
output.append(line)
433+
incode = False
434+
else:
435+
if iscode:
436+
nested_list = sline.startswith(('-', '*', '+')) or sline[0].isdigit()
437+
x = line[4:]
438+
if nested_list:
439+
output.append(line)
440+
elif x.startswith('!'):
441+
output.append('{fence}{language}'.format(
442+
fence=fence, language=x[1:].strip()))
443+
output.append('\n')
444+
else:
445+
output.append(fence+'cpp')
446+
output.append('\n')
447+
output.append(x)
448+
incode = not nested_list
449+
elif isfence:
450+
infence = True
451+
incode = False
452+
output.append(line)
453+
else:
454+
output.append(line)
455+
return '\n'.join(output)
456+
410457
def insert_header(lines, f):
411458
for line in lines:
412459
yield line
@@ -428,6 +475,9 @@ def extract_doc(app, docname, source):
428475
lines = source[0].split('\n')
429476
md = [line[len(extract_prefix):] for line in lines if line.startswith(extract_prefix)]
430477
source[0] = '\n'.join(insert_header(md, os.path.relpath(path, include_dir)))
478+
if path.endswith(('.hpp', '.md')):
479+
lines = source[0].split('\n')
480+
source[0] = convert_to_code_fences(lines)
431481

432482
# app setup hook
433483
def setup(app):

0 commit comments

Comments
 (0)