Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
berumuron
Boop
Commits
3702db82
Commit
3702db82
authored
Feb 24, 2019
by
berumuron
Browse files
Extract a function to write articles and pages
parent
05c9ee86
Changes
4
Hide whitespace changes
Inline
Side-by-side
boop.py
View file @
3702db82
...
...
@@ -100,26 +100,16 @@ def main(environment):
# STEP 2: we write all the pages and articles
for
page
in
pages
:
output_filepath
=
os
.
path
.
join
(
output_path
,
f
"
{
page
.
slug
()
}
.html"
)
# Create dir structure for the file
utils
.
mkdirs_for_file
(
output_filepath
)
# And write the file
with
open
(
output_filepath
,
"w"
)
as
output_file
:
output_file
.
write
(
page
.
render
())
utils
.
write_content
(
page
,
output_path
)
print
(
f
"Written page:
{
page
.
url
()
}
"
)
#
S
ort the articles by publication date
#
We want to s
ort the articles by publication date
first
articles
.
sort
(
key
=
lambda
article
:
article
.
date
(),
reverse
=
True
)
for
serie_articles
in
articles_by_series
.
values
():
serie_articles
.
sort
(
key
=
lambda
article
:
article
.
date
(),
reverse
=
True
)
# 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
(
article
.
render
())
utils
.
write_content
(
article
,
output_path
)
print
(
f
"Written article:
{
article
.
url
()
}
"
)
# Write the page with the list of articles if blog template exists
...
...
boop/article.py
View file @
3702db82
import
os
import
markdown
import
datetime
import
uuid
import
pytz
import
boop.boopsy
as
boopsy
class
Article
:
def
__init__
(
self
,
slug
,
article_content
,
configuration
=
{},
serie
=
None
):
...
...
@@ -31,12 +28,6 @@ class Article:
self
.
meta
[
"ARTICLE_CONTENT"
]
=
content
self
.
meta
[
"ARTICLE_SERIE"
]
=
serie
# And we initialize a template for the article
article_template_filepath
=
os
.
path
.
join
(
"templates"
,
f
"
{
self
.
meta
[
'ARTICLE_TEMPLATE'
]
}
.html"
)
self
.
template
=
boopsy
.
Template
(
article_template_filepath
)
def
render
(
self
):
return
self
.
template
.
render
(
self
.
meta
)
...
...
@@ -88,3 +79,6 @@ class Article:
def
serie
(
self
):
return
self
.
meta
[
"ARTICLE_SERIE"
]
def
template
(
self
):
return
self
.
meta
[
"ARTICLE_TEMPLATE"
]
boop/page.py
View file @
3702db82
import
os
import
uuid
import
re
import
yaml
import
boop.boopsy
as
boopsy
class
Page
:
def
__init__
(
self
,
slug
,
page_content
,
configuration
=
{}):
...
...
@@ -20,20 +17,6 @@ class Page:
self
.
meta
[
article_key
]
=
value
self
.
meta
[
"PAGE_CONTENT"
]
=
page_content
# And we initialize a template for the page
page_template_filepath
=
os
.
path
.
join
(
"templates"
,
f
"
{
self
.
meta
[
'PAGE_TEMPLATE'
]
}
.html"
)
self
.
template
=
None
if
os
.
path
.
exists
(
page_template_filepath
):
self
.
template
=
boopsy
.
Template
(
page_template_filepath
)
def
render
(
self
):
if
self
.
template
is
None
:
return
self
.
content
()
else
:
return
self
.
template
.
render
(
self
.
meta
)
def
slug
(
self
):
return
self
.
meta
[
"PAGE_SLUG"
]
...
...
@@ -50,6 +33,9 @@ class Page:
def
content
(
self
):
return
self
.
meta
[
"PAGE_CONTENT"
]
def
template
(
self
):
return
self
.
meta
[
"PAGE_TEMPLATE"
]
YAML_FRONT_MATTER_REGEX
=
re
.
compile
(
"(---
\n
.*
\n
)(---
\n
)(.*)"
,
re
.
M
|
re
.
S
)
...
...
boop/utils.py
View file @
3702db82
import
os
import
shutil
import
boop.boopsy
as
boopsy
from
boop.article
import
Article
from
boop.page
import
Page
...
...
@@ -66,6 +67,26 @@ def build_page_from_filepath(filepath, configuration, basename=None, slug=None):
return
Page
(
final_slug
,
page_file
.
read
(),
configuration
=
configuration
)
def
write_content
(
content
,
output_path
):
"""Write content (Article or Page) under output_path.
"""
output_filepath
=
os
.
path
.
join
(
output_path
,
f
"
{
content
.
slug
()
}
.html"
)
template_filepath
=
os
.
path
.
join
(
os
.
curdir
,
"templates"
,
f
"
{
content
.
template
()
}
.html"
)
# Initialize the template defined within content
if
os
.
path
.
exists
(
template_filepath
):
template
=
boopsy
.
Template
(
template_filepath
)
rendered_content
=
template
.
render
(
content
.
meta
)
else
:
rendered_content
=
content
.
content
()
mkdirs_for_file
(
output_filepath
)
with
open
(
output_filepath
,
"w"
)
as
output_file
:
output_file
.
write
(
rendered_content
)
def
list_dirs
(
path
):
"""List all the directories within a path.
"""
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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