-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx_bibliography.py
61 lines (44 loc) · 1.46 KB
/
mdx_bibliography.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /usr/bin/env python
'''
Bibliography Extension for Python-Markdown
===============================================
Adds support for bibliography.
Usage
-----
'''
import logging
import markdown
try:
from markdown import etree
except ImportError:
from markdown.util import etree
from markdown.treeprocessors import Treeprocessor
from markdown.inlinepatterns import Pattern
import re
logger = logging.getLogger(__name__)
class BibliographyPattern(Pattern):
def __init__(self):
markdown.inlinepatterns.Pattern.__init__(self, r'{\s*bib:\s*(?P<id>[^\s}]*)[\s]*}')
def handleMatch(self, m):
node = markdown.util.etree.Element('bib')
node.set('key', markdown.util.AtomicString(m.group('id')))
return node
class BibliographyExtension(markdown.Extension):
def __init__(self, configs):
super(BibliographyExtension, self).__init__(configs)
cfg = {}
try:
for (key, val) in self.config:
cfg[key] = val
except:
for (key, val) in self.config.items():
cfg[key] = val[0]
self.config = cfg
def extendMarkdown(self, md, md_globals):
self.md = md
md.inlinePatterns.add('bibreferences', BibliographyPattern(), '_begin')
def makeExtension(*args, **configs):
if len(args) > 0 and isinstance(args[0], dict):
return BibliographyExtension(configs=args[0])
else:
return BibliographyExtension(configs=configs)