#!/usr/bin/env python
# encoding=utf8
"""
Seth Brown
02-24-12
"""
import editor
import workflow
from sys import stdin, stdout
import argparse
import re
from collections import OrderedDict
class ForMd(object):
""" Format Markdown text"""
def __init__(self, text):
super(ForMd, self).__init__()
self.text = text
self.match_links = re.compile(r"""(\[[^^]*?\])\s? # text
(\[.*?\]|\(.*?\r?\n?.*?\)\)?) # ref/url
""", re.MULTILINE | re.X)
self.match_refs = re.compile(r'(?<=\n)\[[^^\r?\n]*?\]:\s?.*')
self.data = []
def _links(self):
""" Find Markdown links"""
links = re.findall(self.match_links, self.text)
for (text, ref) in links:
ref = ref.replace('\n', '').replace('\r', '')
yield (text, ref)
def _refs(self):
""" Find Markdown references"""
refs = re.findall(self.match_refs, self.text)
refs.sort()
refs = OrderedDict(i.split(":", 1) for i in refs)
return refs
def _format(self):
""" Process text"""
links = (i for i in self._links())
refs = self._refs()
for n, link in enumerate(links):
text, ref = link
ref_num = ''.join(("[",str(n+1),"]: "))
if ref in refs.keys():
url = refs.get(ref).strip()
formd_ref = ''.join((ref_num, url))
formd_text = ''.join((text, ref_num))
self.data.append([formd_text, formd_ref])
elif text in refs.keys():
url = refs.get(text).strip()
formd_ref = ''.join((ref_num, url))
formd_text = ''.join((text, ref_num))
self.data.append([formd_text, formd_ref])
elif ref not in refs.keys():
# remove the leading/training parens
parse_ref = ref[1:-1]
formd_ref = ''.join((ref_num, parse_ref))
formd_text = ''.join((text,ref_num))
self.data.append([formd_text, formd_ref])
def inline_md(self):
""" Generate inline Markdown"""
self._format()
text_link = iter([''.join((_[0].split("][",1)[0],
"](", _[1].split(":",1)[1].strip(), ")")) for _ in self.data])
formd_text = self.match_links.sub(lambda _: next(text_link), self.text)
formd_md = self.match_refs.sub('', formd_text).strip()
return formd_md
def ref_md(self):
""" Generate referenced Markdown"""
self._format()
ref_nums = iter([_[0].rstrip(" :") for _ in self.data])
formd_text = self.match_links.sub(lambda _: next(ref_nums), self.text)
formd_refs = self.match_refs.sub('', formd_text).strip()
references = (i[1] for i in self.data)
formd_md = '\n'.join((formd_refs,
'\n', '\n'.join(i for i in references)))
return formd_md
def flip(self):
""" Convert Markdown to the opposite style of the first text link"""
try:
first_match = re.search(self.match_links, self.text).group(0)
if first_match is None or first_match == []:
formd_md = self.text
elif '(' and ')' in first_match:
formd_md = self.ref_md()
else:
formd_md = self.inline_md()
except AttributeError:
formd_md = self.text
yield formd_md
def main():
description = 'formd: A (for)matting (M)ark(d)own tool.'
p = argparse.ArgumentParser(description=description)
p.add_argument('-r', '--ref', help="convert text to referenced Markdown",
action='store_true', default=False)
p.add_argument('-i', '--inline', help="convert text to inline Markdown",
action='store_true', default=False)
p.add_argument('-f', '--flip', help="convert to opposite style Markdown",
action='store_true', default=True)
args = p.parse_args()
md = stdin.read()
text = ForMd(md)
if (args.inline):
[stdout.write(t) for t in text.inline_md()]
elif (args.ref):
[stdout.write(t) for t in text.ref_md()]
elif (args.flip):
[stdout.write(t) for t in text.flip()]
def editorial_ref_md():
text = workflow.get_input()
f = ForMd(text)
ref_md = f.ref_md()
workflow.set_output(ref_md)
editorial_ref_md()
There are no comments yet.