-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import json | ||
import sys | ||
import re | ||
import hashlib | ||
|
||
STORY_REGEX = re.compile("{{((\w+).)?(png|gif) (.+)}}") | ||
|
||
if __name__ == '__main__': | ||
args = sys.argv | ||
if len(args) > 1 and args[1] == "supports": | ||
sys.exit(0) | ||
|
||
context, book = json.load(sys.stdin) | ||
|
||
# all the rendering jobs that need to be done | ||
jobs = {} | ||
|
||
for section in book['sections']: | ||
if not 'Chapter' in section: | ||
continue | ||
|
||
content = section['Chapter']['content'] | ||
replace = [] | ||
|
||
for ref in STORY_REGEX.finditer(content): | ||
type_ = ref.group(3) | ||
filename = ref.group(2) | ||
command = ref.group(4) | ||
if len(command) == 0: | ||
continue | ||
|
||
# The filename is the hash of the args, or can be specified in | ||
# Markdown | ||
if not filename: | ||
h = hashlib.new('sha256') | ||
h.update(command.encode('utf-8')) | ||
filename = h.hexdigest()[:12] | ||
|
||
filename += "." + type_ | ||
|
||
jobs[filename] = command | ||
replace.append( | ||
( | ||
ref.start(0), | ||
ref.end(0), | ||
f"![{command}](./stories/{filename})", | ||
) | ||
) | ||
|
||
for start, end, text in reversed(replace): | ||
content = content[:start] + text + content[end:] | ||
|
||
section['Chapter']['content'] = content | ||
|
||
print(json.dumps(book)) |