Skip to content

Instantly share code, notes, and snippets.

@tcarreira
Last active February 6, 2021 20:12
Show Gist options
  • Save tcarreira/dc75ab8e560a1134fbdd1fd280405e78 to your computer and use it in GitHub Desktop.
Save tcarreira/dc75ab8e560a1134fbdd1fd280405e78 to your computer and use it in GitHub Desktop.
This script prints a mediawiki style Journaling page for the current year.
#!/usr/bin/env python3
# run with: python3 get_mediawiki_journal_year.py
# or send to the clipboard using: python3 get_mediawiki_journal_year.py | xclip -selection clipboard
# source: https://gist.github.com/tcarreira/dc75ab8e560a1134fbdd1fd280405e78
"""
This script prints a mediawiki style Journaling page.
"""
import datetime
import os
import time
FIRST_RECORD_YEAR = 2019
def print_header():
print(
"""
<div class="goto_prev_week">&rarr; Go to previous week</div>
<div class="goto_this_week">&rarr; Go to this week</div>
__FORCETOC__
"""
)
def print_self_code_on_comment():
"Print this script file, commented inside html tags"
# escape comment tag inside the comment
print(f"\n{'<'}!--\n")
with open(__file__) as f:
print(f.read())
print(f"\n--{'>'}\n")
def get_start_week_and_datetime(year):
"""Return tuple (week,first_day)
week = 0 or 1, the one that includes 1/jan
first_day = first day of 'week'
"""
d = datetime.datetime(year=year, month=1, day=1)
if d.weekday() <= 3:
return 1, d - datetime.timedelta(d.weekday())
return 0, d - datetime.timedelta(d.weekday())
def print_weeks_tops(year):
print(f"== {year} Journal == ")
print_self_code_on_comment()
print("")
first_week, day = get_start_week_and_datetime(year)
last_month = ""
for week in range(first_week, 54):
month = (day + datetime.timedelta(days=3)).strftime("%B %Y")
if month != last_month:
last_month = month
print(f"=== {month} ===")
start_day_str = day.strftime("%d %b")
end_day_str = (day + datetime.timedelta(days=6)).strftime("%d %b")
print(f"==== Week {week} ({start_day_str} - {end_day_str}) ====")
print(" = \n + \n - \n")
day = day + datetime.timedelta(days=7)
def print_months_tops(year):
print(f"\n== Monthly highlights {year} ==")
for month in range(1, 13):
d = datetime.datetime(year=year, month=month, day=1)
print(f"=== {d.strftime('%B')} ===")
print(" = \n + \n - \n")
print()
def print_footer(year):
print(
"""
== Symbols ==
= Simple record
+ Positive record
- Negative record
"""
)
print("== Previous records ==\n")
for y in range(FIRST_RECORD_YEAR, year):
print(f"* [[journal_{y}|{y}]]")
print()
def main(year):
print_header()
print_weeks_tops(year)
print_months_tops(year)
print_footer(year)
if __name__ == "__main__":
year = int(os.environ.get("YEAR", datetime.datetime.now().year))
main(year)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment