2022-03-03 18:31:37 +00:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- python -*-
|
|
|
|
|
|
|
|
|
|
from mdbook_plugin import Plugin
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import sqlite3
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
PSEUDOCODE_TAG = r'pseudocode'
|
|
|
|
|
|
|
|
|
|
class PseudocodePlugin(Plugin):
|
|
|
|
|
def expand_chapter_content(self, chapter):
|
|
|
|
|
chapter.content = self.expand_codeblock(PSEUDOCODE_TAG, self.expand_pseudocode, chapter.content)
|
|
|
|
|
|
|
|
|
|
def expand_pseudocode(self, m):
|
|
|
|
|
source = m.group(1)
|
|
|
|
|
source_lines = source.split('\n')
|
|
|
|
|
max_linenumber_len = len(str(len(source_lines)))
|
|
|
|
|
chunks = []
|
|
|
|
|
linenumber = 1
|
|
|
|
|
chunks.append('<span class="pseudocode">')
|
|
|
|
|
for line in source_lines:
|
|
|
|
|
if line:
|
|
|
|
|
line = f' `{str(linenumber).rjust(max_linenumber_len)}:` ' + \
|
2022-03-04 09:47:47 +00:00
|
|
|
|
re.sub('^ *', lambda m: ' ' * (len(m.group(0)) * 2), line) + \
|
2022-03-03 18:31:37 +00:00
|
|
|
|
' \n'
|
|
|
|
|
linenumber = linenumber + 1
|
|
|
|
|
else:
|
|
|
|
|
line = '</span>\n\n<span class="pseudocode">'
|
|
|
|
|
chunks.append(line)
|
|
|
|
|
chunks.append('</span>\n')
|
|
|
|
|
output = ''.join(chunks)
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
PseudocodePlugin().main(sys.argv)
|