Created
November 8, 2017 17:03
-
-
Save ian-holden/c56d16a5f75cfd4f80ed4d30a021aaad to your computer and use it in GitHub Desktop.
Create a Markdown doc from an exported Trello card
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read an exported trello card and create a markdown doc for the main information in the card | |
# including the name, url, description, checklists and all comments and attachments | |
# | |
# usage: | |
# | |
# export the card to a JSON file (Share & more... > Export JSON) | |
# | |
# python trello-card-to-md.py trello-card.json | |
# | |
import json, sys | |
from pprint import pprint | |
card = sys.argv[1] | |
with open(card) as data_file: | |
carddata = json.load(data_file) | |
name=carddata['name'] | |
url=carddata['url'] | |
desc=carddata['desc'] | |
actions = carddata['actions'] | |
checklists=carddata['checklists'] | |
markdown = "# %s\n\n[From Trello card](%s)\n\n---\n\n%s\n\n---\n\n## Checklists:\n\n" % (name, url, desc) | |
def get_checkitem_markdown(checkitems): | |
markdown = "\n" | |
for item in sorted(checkitems, key=lambda k: k['pos']): # sort the list by the item pos | |
markdown += '* ' | |
if item['state'] == 'complete': | |
markdown += '[DONE]- ' | |
else: | |
markdown += '[ ]- ' | |
markdown += item['name'] + "\n" | |
return markdown + "\n" | |
def get_attachments_markdown(actions): | |
markdown = '' | |
for action in sorted(actions, key=lambda k: k['date']): # sort the list by the action date | |
timestamp = action['date'] | |
ts = timestamp[0:10] + ' ' + timestamp[11:16] | |
if action['type'] == 'addAttachmentToCard': # we only care about addAttachmentToCard actions | |
url = action['data']['attachment']['url'] | |
name = action['data']['attachment']['name'] | |
# add a link to the file | |
markdown += "* %s - [%s](%s)\n" % (ts, name, url) | |
# show a preview if there is one | |
if 'previewUrl' in action['data']['attachment']: | |
preview_url = action['data']['attachment']['previewUrl'] | |
markdown += "* ![%s](%s)\n" % (name, preview_url) | |
return markdown | |
def get_comments_markdown(actions): | |
markdown = '' | |
for action in sorted(actions, key=lambda k: k['date']): # sort the list by the action date | |
timestamp = action['date'] | |
ts = timestamp[0:10] + ' ' + timestamp[11:16] | |
if action['type'] == 'commentCard': # we only care about commentCard actions | |
markdown += "* %s - %s\n" % (ts, action['data']['text']) | |
return markdown | |
for checklist in sorted(checklists, key=lambda k: k['pos']): # sort the list by the checklist pos | |
markdown += "### %s\n%s" % (checklist['name'], get_checkitem_markdown(checklist['checkItems'])) | |
markdown += "\n## Comments:\n\n%s" % get_comments_markdown(actions) | |
markdown += "\n\n## Attachments:\n\n%s" % get_attachments_markdown(actions) | |
print markdown; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment