Created
January 25, 2011 20:33
-
-
Save navilan/795591 to your computer and use it in GitHub Desktop.
Textmate Command to extract the link references from a markdown file and insert it as a snippet at cursor.
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
#!/usr/bin/env python -S | |
# coding: utf-8 | |
""" | |
Textmate Command to extract the link references from a | |
markdown file and insert it as a snippet at cursor. | |
Screenshot: http://embr.it/Bp9 | |
""" | |
import re | |
from sys import stdin, stdout | |
short = re.compile(r'\[([^]]*)\]\[\]') | |
link_ref = re.compile(r'\[[^]]*\]\[([^\]]*)\]') | |
txt = stdin.read() | |
matches = short.findall(txt) | |
matches.extend(link_ref.findall(txt)) | |
matches = list(set(matches)) | |
matches.remove('') | |
matches.sort() | |
count = 1 | |
refs = '' | |
for ref in matches: | |
refs = refs + '[%(ref)s]: ${%(count)d:%(ref)s}\n' % (dict(ref=ref, count=count)) | |
count += 1 | |
stdout.write(refs) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment