Recently I've been writing a lot of Markdown. While not as sophisticated as ReStructuredText, it's simplicity is nice for accelerated writing. The problem is that I like to put section headings in titlecase.
What do I mean by titlecase?
go to the room
becomes:
Go to the Room
See how verb 'Go' and the noun 'Room' have their first letter capitalized? And the 'small words', specifically 'to' (preposition) and 'the' (definate article) are not? That's how title casing works (at least in English). That's what I like to see in my section headings.
In theory one could just use Python's str.title()
method to perform
this transformation. However, that method is too global in reach. We
would end up with:
Go To The Room # 'to' and 'the' have titlecase, when they should not.
So how do I programmatically (i.e. quickly) ensure that dozens of files scattered across multiple directories have section headers that are accurately titlecased?
The solution to this is a handy library called titlecase. Empowered by this tool I wrote the following script that allows me to transforms all my markdown files to have titlecased section headings.
"""
Titlecases all markdown section headers in a directory.
Confirmed to work with Python 2.7, 3.3, and 3.4.
Usage:
python titlemd.py a-directory/
python titlemd.py # defaults to '.'
"""
import fnmatch
import os
import sys
try:
from titlecase import titlecase
except ImportError:
print("Please install titlecase")
def main(location):
for root, dirs, files in os.walk(location):
for item in fnmatch.filter(files, "*.md"):
file_path = os.path.join(root, item)
print(file_path)
# Open the file and read the lines as a list
with open(file_path) as f:
lines = f.readlines()
with open(file_path, 'w') as f:
# Loop through the list of lines and titlecase
# any line beginning with '#'.
for line in lines:
if line.strip().startswith('#'):
line = titlecase(line)
f.write(line)
if __name__ == "__main__":
try:
main(sys.argv[1])
except IndexError:
main('.')
If you read this far, you might want to follow me on twitter or github and subscribe via email below (I'll email you new articles when I publish them).
python django pycon howto twoscoops book audrey class-based-views holidays blog rant python3 resolutions friends family djangocon django-rest-framework capoeira ppoftw cheatsheet eventbrite travel forms hackathon api mongodb argentina cookiecutter pypi flask travel tips for geeks LaTeX pycon-2013-guide meme javascript pyladies ruby philippines europe setup tools consumernotebook markdown pydiversity pypy nasa nodejs clojure php sprint LA Consumer-Notebook OAuth heroku review australia Vue.js mountain coreapi colombia cryptocurrency bitcoin eskrima postgresql djangopackages Cartwheel Web training Two Scoops Academy faceoff RestructuredText brambles packaging binstar conda halloween pumpkin ingredients recipe barcamp WhartonWC jinja2 surgery pyramid europython filepicker joke paypal gondor dotcloud Los Angeles lahackthons casestudy rails vs djangodash science dsf functions meteor perl la wsgi lahackathons los-angeles usability css
If you read this far, you might want to follow me on twitter or github and subscribe via email below (I'll email you new articles when I publish them).
Content Copyright © 2012-2018 Daniel Greenfeld. Proudly harnessed by Mountain, powered by Flask, and rendered by Frozen Flask, all of which take great advantage of Python.
Comments