Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter page #236

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ env/
*.log
*.pyc
**/.DS_Store
src/templates/en/2019/chapters/
src/templates/ja/2019/chapters/
4 changes: 4 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.idea/
.vscode/
venv
env
src/templates/en/2019/chapters/*
src/templates/ja/2019/chapters/*
node_modules/
52 changes: 52 additions & 0 deletions src/config/2019.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,58 @@
"authors",
"developers"
]
},
"tomayac": {
"name": "Tomas",
"tagline": "Web Almanac PM",
"github": "rviscomi",
"teams": [
"authors",
"developers"
]
},
"jeffposnick": {
"name": "Jeff Posnick",
"tagline": "Web Almanac PM",
"github": "rviscomi",
"twitter": "rick_viscomi",
"teams": [
"authors",
"developers"
]
},
"addyosmani": {
"name": "Addy Osmani",
"tagline": "Developer Advocate Google",
"gravatar": "[email protected]",
"github": "rviscomi",
"twitter": "rick_viscomi",
"teams": [
"authors",
"developers"
]
},
"housseindjirdeh": {
"name": "Jeff Posnick",
"tagline": "Developer Advocate Google",
"gravatar": "[email protected]",
"github": "rviscomi",
"twitter": "rick_viscomi",
"teams": [
"authors",
"developers"
]
},
"mathiasbynens": {
"name": "Mathias Bynens",
"tagline": "Web Almanac PM",
"gravatar": "[email protected]",
"github": "rviscomi",
"twitter": "rick_viscomi",
"teams": [
"authors",
"developers"
]
}
}
}
122 changes: 106 additions & 16 deletions src/generate_chapters.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
import os
import re
import mistune
from mistune_contrib.toc import TocMixin
import yaml
from visualisation_lexer import VisualisationLexer

renderer = mistune.Renderer()
inline = VisualisationLexer(renderer).enable()
markdown = mistune.Markdown(renderer=renderer, inline=inline)
import config as config_util
import jinja2

class TocRenderer(TocMixin, mistune.Renderer):

def header(self, text, level, raw=None):
title = re.sub('[\W_]+', ' ', text, flags=re.UNICODE).lower().replace(' ','-')
rv = '<h%d id="toc-%s">%s</h%d>\n' % (
level, title, text, level
)
self.toc_tree.append((self.toc_count, text, level, raw))
self.toc_count += 1
return rv

def _iter_toc(self, level):
first_level = 0
last_level = 0

yield '<ul>\n'

for toc in self.toc_tree:
index, text, l, raw = toc
title = re.sub('[\W_]+', ' ', text, flags=re.UNICODE).lower().replace(' ','-')
if l > level:
# ignore this level
continue

if first_level == 0 :
# based on first level
first_level = l
last_level = l
yield '<li><a href="#toc-%s">%s</a>' % (title, text)
elif last_level == l:
yield '</li>\n<li><a href="#toc-%s">%s</a>' % (title, text)
elif last_level == l - 1:
last_level = l
yield '<ul>\n<li><a href="#toc-%s">%s</a>' % (title, text)
elif last_level > l:
# close indention
yield '</li>'
while last_level > l:
yield '</ul>\n</li>\n'
last_level -= 1
yield '<li><a href="#toc-%s">%s</a>' % (title, text)

# close tags
yield '</li>\n'
while last_level > first_level:
yield '</ul>\n</li>\n'
last_level -= 1

yield '</ul>\n'

toc = TocRenderer()
inline = VisualisationLexer(toc).enable()
markdown = mistune.Markdown(renderer=toc, inline=inline)

def generate_chapters():
for language_dir in os.scandir('content'):
Expand All @@ -19,9 +72,8 @@ def generate_chapters():
chapter = re.sub('.md$', '', chapter_file.name)
print('\n Generating chapter: %s, %s, %s' % (chapter, year, language))

(metadata, body) = parse_file(chapter_file)

write_template(language, year, chapter, metadata, body)
(metadata, body, tochtml) = parse_file(chapter_file)
write_template(language, year, chapter, metadata, body, tochtml)


def parse_file(chapter_file):
Expand All @@ -40,22 +92,37 @@ def parse_file(chapter_file):
metadata = yaml.load(metadata_text, Loader=yaml.SafeLoader)

# TODO: Parse the body_text and find placeholders for generating embedded SVG.

body = markdown(body_text)

return (metadata, body)


def write_template(language, year, chapter, metadata, body):
toc.reset_toc()
body = markdown.parse(body_text)
tochtml=toc.render_toc(level=2)
return (metadata, body, tochtml)

def generate_author_html(lang,year, metadata):
config = config_util.get_config(year)
authors = []
for userid in metadata['authors']:
author = config['contributors'].get(userid)
if author is not None:
authors.append(author)

return jinja2.Environment(
loader=jinja2.FileSystemLoader('templates')
).get_template('%s/%s/chapter_author.html' % (lang, year)).render(authors=authors)


def write_template(language, year, chapter, metadata, body, tochtml):
template_path = 'templates/%s/%s/chapter.html' % (language, year)

with open(template_path, 'r') as template_file:
template = template_file.read()

path = 'templates/%s/%s/chapters/%s.html' % (language, year, chapter)


authors = generate_author_html(language, year, metadata)
prevnext = generate_prevnext_html(language, year, metadata['chapter_number'])
with open(path, 'w') as file_to_write:
file_to_write.write(template.format(body=body, metadata=metadata))
file_to_write.write(template.format(body=body, metadata=metadata, toc=tochtml, authors=authors, prevnext=prevnext))
print(' - Output file size: %s' % size_of(file_to_write.tell()))


Expand All @@ -67,4 +134,27 @@ def size_of(num, suffix='B'):
return "%.1f%s%s" % (num, 'Yi', suffix)


generate_chapters()
def generate_prevnext_html(lang,year,chapter_no):
config = config_util.get_config(year)
chapter_no = str(chapter_no)
prev_chapter = False
next_chapter = False
found = False

for part in config['outline']:
for chapter in part['chapters']:
if found:
next_chapter = chapter
break
elif chapter['chapter'] == chapter_no:
found = True
else:
prev_chapter = chapter
if(found):
break

return jinja2.Environment(
loader=jinja2.FileSystemLoader('templates')
).get_template('%s/%s/chapter_prevnext.html' % (lang, year)).render(lang=lang, year=year, prev_chapter=prev_chapter,next_chapter=next_chapter)

generate_chapters()
49 changes: 42 additions & 7 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
argh==0.26.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this is a lot of new requirements! Where did they all come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was installing the mistune contrib module and trying to update the the requirements but didn't realized it somehow generated all of these :|

astroid==2.3.1
atomicwrites==1.3.0
attrs==19.2.0
Click==7.0
colorama==0.4.1
cycler==0.10.0
docopt==0.6.2
Flask==1.0.2
flask-talisman
flask-talisman==0.7.0
gunicorn==19.9.0
matplotlib
mistune
pandas
pytest
pytest-watch
scour
importlib-metadata==0.23
isort==4.3.21
itsdangerous==1.1.0
Jinja2==2.10.3
kiwisolver==1.1.0
lazy-object-proxy==1.4.2
MarkupSafe==1.1.1
matplotlib==3.1.1
mccabe==0.6.1
mistune==0.8.4
mistune-contrib==0.1
more-itertools==7.2.0
numpy==1.17.2
packaging==19.2
pandas==0.25.1
pathtools==0.1.2
pluggy==0.13.0
py==1.8.0
pylint==2.4.2
pyparsing==2.4.2
pytest==5.2.1
pytest-watch==4.2.0
python-dateutil==2.8.0
pytz==2019.3
PyYAML==5.1.2
scour==0.37
six==1.12.0
typed-ast==1.4.0
watchdog==0.9.0
wcwidth==0.1.7
Werkzeug==0.16.0
wrapt==1.11.2
zipp==0.6.0
Loading