-
Notifications
You must be signed in to change notification settings - Fork 71
/
deckjs.py
executable file
·106 lines (83 loc) · 3.49 KB
/
deckjs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import datetime
import logging
import re
from markdown import markdown
from markdown.preprocessors import Preprocessor
from markdown.extensions import Extension
# TODO: extract this into a module of its own
class CodeSnippets(Extension):
def __init__(self, seek_directory):
self.seek_directory = seek_directory
def extendMarkdown(self, md, md_globals):
preproc = CodeSnippetIncludingPreProcessor(self.seek_directory)
md.preprocessors.add('code_snippets', preproc, '_begin')
class CodeSnippetIncludingPreProcessor(Preprocessor):
def __init__(self, seek_directory):
self.satement_replacers = [CodeIncludeSatementReplacer(seek_directory)]
def run(self, lines):
new_lines = []
for line in lines:
for replacer in self.satement_replacers:
if (replacer.replaces(line)):
new_lines.append(replacer.replacement_for(line))
else:
new_lines.append(line)
return new_lines
class CodeIncludeSatementReplacer(object):
def __init__(self, seek_directory):
self.include_statement = re.compile(r"@(\(([^\)]*)\))?\[([^\]]*)\]")
self.seek_directory = seek_directory
def replaces(self, line):
return self.include_statement.match(line) != None
def replacement_for(self, line):
parameters = self.include_statement.search(line)
include_file = parameters.group(3)
if not os.path.isabs(include_file):
include_file = os.path.join(self.seek_directory, include_file)
snippet_maker = parameters.group(2)
with open(include_file) as file_content:
if not snippet_maker:
included = file_content.read()
else:
included = self.truncate_to_part_between_maker(snippet_maker, file_content)
# TODO: factor this out as a parameter
return "~~~ {.scala}\n" + included + "\n~~~"
def truncate_to_part_between_maker(self, marker, file_content):
included = []
including = False
for line in file_content:
if marker in line and including:
return "\n".join(included)
if including:
included.append(line)
if marker in line and not including:
including = True
return "\n".join(included)
SLIDES_PATH = 'slides' + os.sep
MARKDOWN_EXTRAS = ['extra', 'codehilite', CodeSnippets(os.getcwd())]
SLIDES = []
def preBuild(site):
global SLIDES
# Build all the slides
for page in site.pages():
if page.path.startswith(SLIDES_PATH):
# TODO: Skip non html slides for obious reasons
# Build a context for each slide
slide_meta_data = {}
slide_meta_data["title"] = "-".join(page.path.split("-")[1:])
slide_meta_data["path"] = page.path
page_content = open(page.paths['full']).read()
page_directory = os.path.dirname(page.paths['full'])
if page.path.endswith(".md"):
slide_meta_data["markdown_content"] = markdown(page_content, ['extra', 'codehilite', CodeSnippets(page_directory)])
SLIDES.append(slide_meta_data)
# Sort the slides by pathname
SLIDES = sorted(SLIDES, key=lambda x: x['path'])
def preBuildPage(site, page, context, data):
"""
Add the list of slides to every page context so we can
access them from wherever on the site.
"""
context['slides'] = SLIDES
return context, data