Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
Boop
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Marien Fressinaud
Boop
Commits
303d1192
Commit
303d1192
authored
Dec 12, 2018
by
Marien Fressinaud
Browse files
Options
Browse Files
Download
Plain Diff
tec: Refactor articles gathering
parents
c37fe01d
207381f1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
33 deletions
+39
-33
boop.py
boop.py
+39
-33
No files found.
boop.py
View file @
303d1192
...
...
@@ -29,6 +29,35 @@ class ProgramError(Exception):
pass
class
Article
:
def
__init__
(
self
,
slug
,
article_content
):
self
.
slug
=
slug
# We convert the file's content into HTML
md
=
markdown
.
Markdown
(
extensions
=
[
"meta"
,
"fenced_code"
,
"attr_list"
])
self
.
content
=
md
.
convert
(
article_content
)
# We get the local variables from the Markdown file (metadata)
# which will be accessible in the article template
self
.
meta
=
{}
for
key
,
values
in
md
.
Meta
.
items
():
article_key
=
f
"ARTICLE_{key.upper()}"
# The Markdown's meta extension extract all the values in arrays, even
# if there is only one value. Because there is no iteration system in
# the template system, for the moment we just get the first value of
# the array.
self
.
meta
[
article_key
]
=
values
[
0
]
# And we initialize a template for the article
article_template_filepath
=
os
.
path
.
join
(
"theme"
,
"article.html"
)
self
.
template
=
boopsy
.
Template
(
article_template_filepath
)
def
render
(
self
):
variables
=
self
.
meta
.
copy
()
variables
[
"ARTICLE_CONTENT"
]
=
self
.
content
return
self
.
template
.
render
(
variables
)
def
dir_tree
(
path
):
"""List all the tree directory under the given path.
"""
...
...
@@ -41,20 +70,6 @@ def dir_tree(path):
yield
filename
def
convert_meta_to_article_vars
(
meta
):
"""Convert Markdown Meta to vars for article layout.
"""
article_vars
=
{}
for
key
,
values
in
meta
.
items
():
article_key
=
f
"ARTICLE_{key.upper()}"
# The Markdown's meta extension extract all the values in arrays, even
# if there is only one value. Because there is no iteration system in
# the template system, for the moment we just get the first value of
# the array.
article_vars
[
article_key
]
=
values
[
0
]
return
article_vars
def
main
():
# Make sure ./site folder is empty
output_path
=
os
.
path
.
join
(
os
.
curdir
,
"site"
)
...
...
@@ -85,34 +100,25 @@ def main():
# If not, just stop here
return
md
=
markdown
.
Markdown
(
extensions
=
[
"meta"
,
"fenced_code"
,
"attr_list"
])
# And copy the files from ./articles to ./site
# List files from ./articles
articles
=
[]
for
filename
in
dir_tree
(
articles_path
):
base
,
ext
=
os
.
path
.
splitext
(
filename
)
slug
,
ext
=
os
.
path
.
splitext
(
filename
)
if
ext
!=
".md"
:
# Don't consider files which are not Markdown
next
# We read the article file and initialize an Article from it
article_filepath
=
os
.
path
.
join
(
articles_path
,
filename
)
output_filepath
=
os
.
path
.
join
(
output_path
,
f
"{base}.html"
)
# We convert the file's content into HTML
with
open
(
article_filepath
,
"r"
)
as
article_file
:
html
=
md
.
convert
(
article_file
.
read
())
# We initialize the template
article_template_filepath
=
os
.
path
.
join
(
"theme"
,
"article.html"
)
template
=
boopsy
.
Template
(
article_template_filepath
)
# We get the local variables from the Markdown file (metadata)
# which will be accessible in the article template
article_vars
=
convert_meta_to_article_vars
(
md
.
Meta
)
article_vars
[
"ARTICLE_CONTENT"
]
=
html
article
=
Article
(
slug
,
article_file
.
read
())
articles
.
append
(
article
)
# And we write the content in the output file
# And write them in the ./site folder
for
article
in
articles
:
output_filepath
=
os
.
path
.
join
(
output_path
,
f
"{article.slug}.html"
)
with
open
(
output_filepath
,
"w"
)
as
output_file
:
output_file
.
write
(
template
.
render
(
article_vars
))
output_file
.
write
(
article
.
render
(
))
if
__name__
==
"__main__"
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment