Skip to content

Commit

Permalink
Merge pull request #7 from UCLH-Foundry/jekyllify-docs
Browse files Browse the repository at this point in the history
Jekyllify docs
  • Loading branch information
tcouch authored Dec 15, 2023
2 parents fc709e3 + aefdc42 commit 7438865
Show file tree
Hide file tree
Showing 35 changed files with 127 additions and 110 deletions.
5 changes: 2 additions & 3 deletions _includes/project_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
<h5>{{project.title}}</h5>

<small class="text-muted">
{%- if project.authors -%} {%- for author in project.authors -%} {%-
assign person = site.data.people[author] -%}
{%- if project.authors -%} {%- for author in project.authors -%}
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<span class="p-author h-card" itemprop="name"
>{{ person.name }}</span
>{{ author }}</span
></span
>
{%- if forloop.last == false -%}, {%- endif -%} {%- endfor -%} {%- endif
Expand Down
3 changes: 1 addition & 2 deletions _layouts/project.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ <h1 class="post-title p-name" itemprop="name headline">{{ page.title | escape }}
{%- endif -%}
{%- if page.authors -%}
• {% for author in page.authors %}
{% assign person = site.data.people[author] %}
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<span class="p-author h-card" itemprop="name">{{ person.name }}</span></span>
<span class="p-author h-card" itemprop="name">{{ author }}</span></span>
{%- if forloop.last == false %}, {% endif -%}
{% endfor %}
{%- endif -%}</p>
Expand Down
1 change: 1 addition & 0 deletions _projects/_project_template/01_extra.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
More content
5 changes: 5 additions & 0 deletions _projects/_project_template/02_data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<body>
<h1>Data</h1>
<p>Here's our synthetic data and code</p>
<iframe src="https://imgs.xkcd.com/comics/data_trap.png"></iframe>
</body>
2 changes: 2 additions & 0 deletions _projects/_project_template/authors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Alan Ahlberg
Janet Ahlberg
4 changes: 0 additions & 4 deletions _projects/_project_template/extra.html

This file was deleted.

23 changes: 0 additions & 23 deletions _projects/_project_template/index.md
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
---
title: # project title
tags: # space separated list
layout: project
# Add author data to _data/people and list here:
authors:
- author_1
# Add additional tabs by linking to other sources
tabs:
- {
name: 'extra', # Two tabs cannot share the same name
type: 'html', # Optional: embed/html/markdown
source: 'extra.html', # filename or embed url
label: 'Extra tab' # Optional: label for tab, defaults to capitalised name
}
- {
name: 'data',
type: 'embed',
source: 'https://www.explainxkcd.com/wiki/images/d/d7/flawed_data.png',
label: 'Data and Code' # Optional: label for tab, defaults to capitalised name
}
---

Write your project overview in markdown format here
1 change: 1 addition & 0 deletions _projects/cheese_therapy/_data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<iframe src="https://widgets.figshare.com/articles/18692549/embed?show_title=1" width="568" height="351" allowfullscreen frameborder="0"></iframe>
2 changes: 2 additions & 0 deletions _projects/cheese_therapy/authors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Anne Onamus
Professor Plum
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
title: Cheese therapy
tags: cheese
layout: project
title: Cheese Therapy
authors:
- author_1
- Anne Onamus
- Professor Plum
tabs:
- {
name: 'appendix',
type: 'embed',
source: 'https://widgets.figshare.com/articles/18692549/embed?show_title=1'
}
- {
name: laaitepn,
type: html,
source: _data.html,
label: Data
}
---

Stinking bishop manchego cheese triangles. Monterey jack when the cheese comes out everybody's happy goat cream cheese roquefort gouda cheese triangles cheeseburger. Melted cheese lancashire airedale macaroni cheese mozzarella babybel stilton port-salut. Camembert de normandie ricotta cheese triangles who moved my cheese mascarpone manchego roquefort cheese slices. Lancashire.
Expand Down
1 change: 1 addition & 0 deletions _projects/fainting_goats/_03_data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<iframe src="https://widgets.figshare.com/articles/17025359/embed?show_title=1" width="568" height="351" allowfullscreen frameborder="0"></iframe>
2 changes: 2 additions & 0 deletions _projects/fainting_goats/authors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Professor Plum
Doctor Foster
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
---
title: Fainting goats
tags: goats fainting
layout: project
title: Fainting Goats
authors:
- author_1
- author_3
- Professor Plum
- Doctor Foster
tabs:
- {
name: 'data',
type: 'embed',
source: 'https://widgets.figshare.com/articles/17025359/embed?show_title=1'}
- {
name: qlcxqfnn,
type: html,
source: _03_data.html,
label: 03 Data
}
---

The myotonic goat or Tennessee fainting goat is an American breed of goat. It is characterised by myotonia congenita, a hereditary condition that may cause it to stiffen or fall over when excited or startled. It may also be known as the fainting goat, falling goat, stiff-legged goat or nervous goat, or as the Tennessee wooden-leg goat. Four goats of this type were brought to Tennessee in the 1880s.
Expand Down
2 changes: 2 additions & 0 deletions _projects/mega_project_1/authors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Anne Onamus
Sue Donnymous
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Test Testerson
Trial Runterton
Trial Runner
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
---
layout: project
title: Example Project A
title: Scurvy Time
authors:
- Test Testerson
- Trial Runterton
- Trial Runner
tabs:
- {
name: roozwhna,
name: vphbybwh,
type: md,
source: _gdpr.md,
label: Gdpr
source: _analysis.md,
label: Analysis
}
- {
name: ucqrttey,
name: npuufhpv,
type: md,
source: _analysis.md,
label: Analysis
source: _gdpr.md,
label: Gdpr
}
- {
name: ndflidwz,
name: kqfokysy,
type: html,
source: _data.html,
label: Data
Expand Down
55 changes: 44 additions & 11 deletions _utils/jekyllify.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,36 @@ def get_project_dirs(name:str=None)->list[Path]:
else:
raise FileNotFoundError(f"No directory named {name} found under {PROJECTS_DIR}")
else:
return [path for path in PROJECTS_DIR.iterdir() if path.is_dir()]
return [
path for path in PROJECTS_DIR.iterdir()
if
path.is_dir()
and
path.stem[0] != "_"
]

def get_random_string(len:int) -> str:
return ''.join(random.choice(ascii_lowercase) for i in range(len))

def get_tab_source_files(dir:Path) -> list[Path]:
source_files = list(dir.glob("*.md")) + list(dir.glob("*.html"))
# Remove index.md or index.html
for index in [dir/"index.md", dir/"index.html"]:
if index in source_files:
source_files.remove(index)
return source_files

def generate_label_from_filename(stem:str) -> str:
"""Given a filename stem (e.g. 03_code-and_data) convert this to a label by:
1. replacing any - or _ characters with spaces
2. removing any digits at the start of the filename
3. removing any whitespace remaining at the start or end of the name
4. converting the name to title case"""
label = stem.replace("_"," ").replace("-"," ")
label = re.sub("^[\d]*","",label)
label = label.strip()
return label.title()

def generate_title_yaml(dir:Path) -> str:
"""Title can come from title.txt, or the project folder name will be used.
Expand Down Expand Up @@ -107,24 +133,31 @@ def generate_tabs_yaml(dir:Path) -> str:
index.html) and return a yaml block style list of tabs referencing those files.
Attributes type, source, and label are generated from the file name, and name is
a randomly generated string."""
extra_files = list(dir.glob("*.md")) + list(dir.glob("*.html"))
# Remove index.md or index.html as that's already accounted for
for index in [dir/"index.md", dir/"index.html"]:
if index in extra_files:
extra_files.remove(index)
source_files = get_tab_source_files(dir)
tabs = "tabs:\n"
for file in extra_files:
for file in source_files:
tabs += "- {\n"
tabs += f" name: {get_random_string(8)},\n" # Used as html element id
tabs += f" type: {file.suffix[1:]},\n" # Remove the dot from suffix
tabs += f" source: {file.name},\n"
tabs += f" label: {file.stem.replace('_',' ').replace('-',' ').strip().title()}\n"
tabs += f" label: {generate_label_from_filename(file.stem)}\n"
tabs += " }\n"
return tabs

def prepend_underscores_to_tab_sources(dir:Path)-> None:
"""For each file in the directory with a .html or .md suffix, rename and prepend
with an underscore character (except for index.md)"""
source_files = get_tab_source_files(dir)
for file in source_files:
if not file.stem[0] == "_":
file.rename(file.with_stem("_"+file.stem))

def jekyllify(dir:Path)->None:
"""Given a project directory, generate yaml front matter and prepend to index.md"""
print(f"Adding front matter to {dir}")
"""Given a project directory, add underscores to any .md or .html files other than
index.md to ensure they do not get processed by Jekyll, then generate yaml front
matter and prepend to index.md."""
print(f"Jekyllifying {dir}")
prepend_underscores_to_tab_sources(dir)
front_matter = "---\n"
front_matter += "layout: project\n" # layout is always project
front_matter += generate_title_yaml(dir)
Expand All @@ -135,7 +168,7 @@ def jekyllify(dir:Path)->None:
content = f.read()
f.seek(0,0)
f.write(front_matter + content)

if __name__=="__main__":
print("Jekyllify script running...")
args = argument_parser.parse_args()
Expand Down
Loading

0 comments on commit 7438865

Please sign in to comment.