Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SWIFTweb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SWIFT
SWIFTweb
Commits
6bddeecf
Commit
6bddeecf
authored
7 years ago
by
Josh Borrow
Browse files
Options
Downloads
Patches
Plain Diff
Upated compiler to use new program flow
parent
ebb3dcff
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
templates/compiler.py
+75
-15
75 additions, 15 deletions
templates/compiler.py
with
75 additions
and
15 deletions
templates/compiler.py
+
75
−
15
View file @
6bddeecf
"""
Short compilation script from the website (essentially just stitches together a bunch of jinja templates)
"""
"""
Short compilation script from the website
(essentially just stitches together a bunch of jinja templates)
Created 16-08-2017 by Josh Borrow (joshua.borrow@durham.ac.uk)
"""
import
jinja2
import
yaml
import
os
def
get_from_filesystem
(
template_names
,
template_dir
=
"
.
"
):
"""
Takes a list of template names and returns a list of template objects
that are ready for rendering.
"""
def
get_from_filesystem
(
template_name
,
template_dir
=
"
.
"
):
"""
Grab a template from the filesystem and return the template object ready for rendering
"""
template_loader
=
jinja2
.
FileSystemLoader
(
searchpath
=
template_dir
)
template_env
=
jinja2
.
Environment
(
loader
=
template_loader
)
return
template_env
.
get_template
(
template_name
)
return
list
(
map
(
template_env
.
get_template
,
template_names
))
def
render_template
(
template
,
data_dir
=
"
../data
"
,
args
=
{}):
"""
Args is a dictionary of the local variables required for
rendering the template.
"""
args
=
args
+
global_variables
(
template
,
data_dir
)
return
template
.
render
(
**
args
)
def
process_navbar
(
raw
,
active
):
"""
Processes the navbar from our favourite friends over at global.yaml
"""
output
=
[]
for
key
,
item
in
raw
[
"
navbar
"
].
iteritems
():
classes
=
[]
def
compile_pubs
(
template_name
=
"
pubs.jinja
"
,
template_dir
=
"
.
"
,
data_dir
=
"
../data
"
):
"""
Render the pubs page
"""
template
=
get_from_filesystem
(
template_name
,
template_dir
)
if
key
!=
len
(
raw
[
"
navbar
"
])
-
1
:
# Lines between items are handled
classes
.
append
(
"
rightborder
"
)
# by borders on the right of items
if
item
[
1
]
==
active
:
# If current template
classes
.
append
(
"
active
"
)
with
open
(
"
{}/pubs.yaml
"
.
format
(
data_dir
),
'
r
'
)
as
f
:
cards
=
yaml
.
load
(
f
)
output
.
append
([
item
[
0
],
item
[
1
],
"
"
.
join
(
classes
)])
r
eturn
template
.
render
(
cards
=
cards
)
r
aw
[
"
navbar
"
]
=
output
return
raw
def
compile_home
(
template_name
=
"
index.jinja
"
,
template_dir
=
"
.
"
,
data_dir
=
"
../data
"
):
"""
Render the homepage
"""
template
=
get_from_filesystem
(
template_name
,
template_dir
)
def
global_variables
(
active
,
data_dir
=
"
../data
"
):
"""
Grabs the global variables from the data directory and sticks them into
a dicitonary ready for template rendering.
Active is the name of the current page
'
s template and is used in the
production of the processed navbar.
"""
return
template
.
render
()
with
open
(
"
{}/global.yaml
"
.
format
(
data_dir
),
"
r
"
)
as
f
:
raw
=
yaml
.
load
(
f
)
# We need to process the navbar.
return
process_navbar
(
raw
,
active
)
def
render
(
render_pages
,
data_dir
,
template_dir
,
output_dir
):
"""
Main rendering function for all of the pages.
"""
output
=
[
render_template
(
page
,
data_dir
)
for
page
in
render_pages
]
# Save the files into the output_dir
if
not
os
.
path
.
exists
(
output_dir
):
os
.
makedirs
(
output_dir
)
for
page
,
data
in
zip
(
render_pages
,
output
):
with
open
(
"
{}/{}
"
.
format
(
output_dir
,
page
),
"
r
"
)
as
f
:
f
.
write
(
data
)
return
if
__name__
==
"
__main__
"
:
print
(
compile_home
())
render_pages
=
[
"
index.html
"
,
"
pubs.html
"
]
render
(
render_pages
,
data_dir
=
"
../data
"
,
template_dir
=
"
.
"
,
output_dir
=
"
./compiled
"
,
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment