40 lines
1.1 KiB
Plaintext
40 lines
1.1 KiB
Plaintext
|
#!/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)}:` ' + \
|
|||
|
re.sub('^ *', lambda m: ' ' * len(m.group(0)), line) + \
|
|||
|
' \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)
|