Skip to content

Instantly share code, notes, and snippets.

@mx-psi
Created June 26, 2018 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mx-psi/b655cd53d325411a12eb8d6174a2dee6 to your computer and use it in GitHub Desktop.
Save mx-psi/b655cd53d325411a12eb8d6174a2dee6 to your computer and use it in GitHub Desktop.
Script I wrote to convert the log of coDual to a Jekyll collection
#! /usr/bin/python3
# Author: Pablo Baeyens
# Usage: ./weeks.py
from datetime import date, timedelta
import platform
import locale
from ruamel.yaml import YAML
yaml = YAML()
SUNDAY = timedelta(days = 6)
name = lambda dt: dt.strftime("%d de %B")
def get_title(dt):
"""Obten título de la semana"""
n = dt.isocalendar()[1]
start, end = name(dt), name(dt + SUNDAY)
return "Semana {0} ({1} a {2})".format(n,start,end)
def build_post(dt, content):
"""Crea post para una semana dada una fecha."""
name = "s" + '{n:02d}'.format(n = dt.isocalendar()[1]) + ".md"
frontmatter = dict(
title = get_title(dt),
tag = "s" + str(dt.isocalendar()[1]),
date = dt + SUNDAY)
with open(name, 'w') as week_file:
week_file.write("---\n")
yaml.dump(frontmatter, week_file)
week_file.write("---\n")
week_file.write(content)
def read_contents():
"""Read 2018.md contents"""
contents = []
with open("2018.md", 'r') as w2018:
cur_week = ""
for line in w2018.readlines():
if line.startswith("<h3"):
contents.append(cur_week)
cur_week = ""
else:
cur_week += line
return contents
if __name__ == "__main__":
locale.setlocale(locale.LC_ALL, "es_ES.utf8")
contents = read_contents()
cur_monday = date(2018,1,1)
while cur_monday <= date(2018, 6, 24):
week_number = cur_monday.isocalendar()[1]
build_post(cur_monday, contents[week_number])
cur_monday += timedelta(weeks = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment