Skip to content

Instantly share code, notes, and snippets.

@billspat
Created February 2, 2021 20:00
Show Gist options
  • Save billspat/bf743857b512e45f69890320d3062bd0 to your computer and use it in GitHub Desktop.
Save billspat/bf743857b512e45f69890320d3062bd0 to your computer and use it in GitHub Desktop.
simple script to read a TSV file and creaate author pages for Hugo/Wowchemy/Academic theme
#!/usr/bin/env python
#### import_authors.py
# python 3.7+
# create author pages for a Hugo/Wowchemy site ( https://wowchemy.com/ )
# from a TSV file (e.g. download from google sheets) with very specific headers
# import_authors.py <tsv file> <../path/to/site>
# requires a TSV with the following field header row:
# Name ProjectRole Photo InstitutioneRole/Title URL Org1Name Org1URL Org2Name Org2URL one_line_bio interests PhD PhDInstitution PhDYear MS MSInstitution MSYear BS/BA BS/BAInstitution BS/BAYear emailaddress twitterid githuburl googlescholarurl paragraph_bio
# clunky but works for now!
import csv, os.path, argparse, warnings
# jinja2 is only external lib used
from jinja2 import Template, Environment, FileSystemLoader, select_autoescape
def tsv_to_authors(people_file, site_folder, author_path="content/authors"):
"""read in tsv file, for each row create an author entry (index.md)
for the Hugo template system from 'wowchemmy'
formerly academia.
1, Creates author folders by using only alphanumeric chars from author names
2. creates index.md file in each folder OVERWRITES FILES WITHOUT WARNING
does not delete any existing folders
does not copy photos over
requires a template file
requires column names to match template
"""
author_path = os.path.join(site_folder, "content/authors")
#env = Environment(
# loader=FileSystemLoader(template_path),
# autoescape=select_autoescape(['html', 'xml']),
#)
#template = env.get_template(template_file)
template = Template(author_template)
with open(people_file, "r") as f:
reader = csv.DictReader(f, delimiter="\t") # assumes TSV
for p in reader:
print(p['Name'])
author_folder = "".join([c for c in p['Name'] if c.isalpha()]).rstrip()
author_file = os.path.join(author_path, author_folder, '_index.md')
author_md = template.render(p=p)
# print(author_md)
os.makedirs(os.path.join(author_path, author_folder),exist_ok=True)
with open(author_file, 'w') as the_file:
the_file.write(author_md)
author_template = """
---
# Display name
title: {{ p['Name'] }}
# Is this the primary user of the site?
superuser: false
# Role/position
role: {{ p['InstitutioneRoleTitle'] }}
# Organizations/Affiliations
organizations:
- name: {{ p['Org1Name'] }}
url: "{{ p['Org1URL'] }}"
{% if p['Org2Name']|length -%}
- name: {{ p['Org2Name'] }}
url: "{{ p['Org2URL'] }}"
{%- endif %}
# Short bio (displayed in user profile at end of posts)
bio: {{p['one_line_bio']}}
{% if p['interests']|length %}
interests:
{% for interest in p['interests'].split(',') -%}
- {{interest.strip()}}
{% endfor %}
{%- endif %}
education:
courses:
- course: {{ p['PhD'] }}
institution: {{ p['PhDInstitution'] }}
year: {{p['PhDYear']}}
{%- if p['MS']|length %}
- course: {{p['MS']}}
institution: {{p['MSInstitution']}}
year: {{p['MSYear']}}
{%- endif %}
{%- if p['BS/BA']|length %}
- course: {{p['BS/BA']}}
institution: {{p['BS/BAInstitution']}}
year: {{p['BS/BAYear']}}
{%- endif %}
# Social/Academic Networking
# For available icons, see: https://sourcethemes.com/academic/docs/page-builder/#icons
# For an email link, use "fas" icon pack, "envelope" icon, and a link in the
# form "mailto:your-email@example.com" or "#contact" for contact widget.
social:
- icon: envelope
icon_pack: fas
link: "mailto:{{ p['emailaddress'] }}"
{% if p['twitterid']|length -%}
- icon: twitter
icon_pack: fab
link: "https://twitter.com/{{p['twitterid']}}"
{%- endif %}
{%- if p['googlescholarurl']|length %}
- icon: google-scholar
icon_pack: ai
link: "{{ p['googlescholarurl']}}"
{%- endif -%}
{%- if p['githuburl']|length %}
- icon: github
icon_pack: fab
link: "{{p['githuburl']}}"
{%- endif -%}
# Link to a PDF of your resume/CV from the About widget.
# To enable, copy your resume/CV to `static/files/cv.pdf` and uncomment the lines below.
# - icon: cv
# icon_pack: ai
# link: files/cv.pdf
# Enter email to display Gravatar (if Gravatar enabled in Config)
# email: "{{ p['emailaddress'] }}"
# Highlight the author in author lists? (true/false)
highlight_name: false
# Organizational groups that you belong to (for People widget)
# Set this to `[]` or comment out if you are not using People widget.
user_groups:
- Group Members
{%- if p['Role'] -%}
- p['Role']
{%- endif %}
---
{{p['paragraph_bio']}}
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('people_file', type=str,help="TSV formatted file")
parser.add_argument('site_path',
type=str,
help='root folder of the Academic/Hugo website')
args = parser.parse_args()
# site_folder = "../../srm-ecology-website"
# people_file="srm-ecology-people.tsv"
# template_file="authortemplate.md"
print(f"importing tsv {args.people_file} to {args.site_path}")
tsv_to_authors(people_file=args.people_file, site_folder=args.site_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment