Separate HTML files for each chapter
This commit is contained in:
parent
bd9c845a3d
commit
8f9e04cf95
80
grawlix.py
80
grawlix.py
@ -3,15 +3,18 @@ from collections import defaultdict
|
||||
import random
|
||||
import yaml
|
||||
import chevron
|
||||
import sys
|
||||
|
||||
class GrawlixCodex():
|
||||
|
||||
def __init__(self, width=30, height=10, wordcount=50000):
|
||||
def __init__(self, width=30, height=10, wordcount=50000, chaplength=4000):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.smallest = 1
|
||||
self.longest = 6
|
||||
self.wordcount = wordcount
|
||||
self.chapleast = int(chaplength * 0.8)
|
||||
self.chapmost = int(chaplength * 1.2)
|
||||
|
||||
|
||||
def config(self, cffile):
|
||||
@ -37,38 +40,69 @@ class GrawlixCodex():
|
||||
self.tags = [ t for t in self.groups.keys() if not t == "blank" ]
|
||||
self.blank = list(self.groups["blank"])[0]
|
||||
|
||||
def text(self):
|
||||
for w in range(self.wordcount):
|
||||
l = random.randint(self.smallest, self.longest)
|
||||
for g in range(l):
|
||||
yield random.choice(list(self.groups["glyph"]))
|
||||
if random.randint(1, 10) > 8:
|
||||
yield random.choice(list(self.groups["colour"]))
|
||||
yield(self.blank)
|
||||
def word(self):
|
||||
l = random.randint(self.smallest, self.longest)
|
||||
for g in range(l):
|
||||
yield random.choice(list(self.groups["glyph"]))
|
||||
|
||||
def write(self):
|
||||
def text(self):
|
||||
for w in range(self.wordcount):
|
||||
if w % 1000 == 0:
|
||||
print(f"{w} words", file=sys.stderr)
|
||||
for g in self.word():
|
||||
yield g
|
||||
if random.randint(1, 10) > 8:
|
||||
yield random.choice(list(self.groups["colour"]))
|
||||
yield(self.blank)
|
||||
|
||||
|
||||
def line(self, gs, cls):
|
||||
glyphs = [ f'<img class="{cls}" src="img/{g}.png" />' for g in gs ]
|
||||
return ''.join(glyphs) + "<br />\n"
|
||||
|
||||
def chap_title(self):
|
||||
title = []
|
||||
for w in range(1, random.randint(1, 5)):
|
||||
for g in self.word():
|
||||
title.append(g)
|
||||
title.append(self.blank)
|
||||
title = title[:-1]
|
||||
title.append(random.choice(list(self.groups["colour"])))
|
||||
return title
|
||||
|
||||
|
||||
def chapters(self):
|
||||
chap_length = random.randint(self.chapleast, self.chapmost)
|
||||
n = 0
|
||||
row = []
|
||||
html = ""
|
||||
for g in self.text():
|
||||
row.append(g)
|
||||
if len(row) == self.width:
|
||||
glyphs = [ f'<img class="gl" src="img/{g}.png" /> ' for g in row ]
|
||||
print(''.join(glyphs))
|
||||
print('<br />')
|
||||
html += self.line(row, "gl")
|
||||
row = []
|
||||
n += 1
|
||||
if n > chap_length:
|
||||
chap_length = random.randint(self.chapleast, self.chapmost)
|
||||
n = 0
|
||||
yield {
|
||||
"title": self.line(self.chap_title(), "ct"),
|
||||
"text": html
|
||||
}
|
||||
|
||||
gc = GrawlixCodex(wordcount=500)
|
||||
gc = GrawlixCodex(wordcount=50000, chaplength=10000)
|
||||
gc.config("./config.yml")
|
||||
gc.load_emojos()
|
||||
|
||||
content = {
|
||||
"front_matter": "Title page",
|
||||
"chapters": [
|
||||
{ "title": "One", "text": "blah blah" },
|
||||
{ "title": "Two", "text": "foo foo" },
|
||||
]
|
||||
}
|
||||
n = 1;
|
||||
|
||||
for chapter in gc.chapters():
|
||||
with open("template.html", "r") as tf:
|
||||
with open(f"output/chapter_{n}.html", "w") as cf:
|
||||
print(chevron.render(tf, chapter), file=cf)
|
||||
print(f"Chapter {n}", file=sys.stderr)
|
||||
n += 1
|
||||
|
||||
|
||||
|
||||
with open("template.html", "r") as tf:
|
||||
print(chevron.render(tf, content))
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user