Commit b3f1f14e authored by Alexandre Leray's avatar Alexandre Leray
Browse files

Removed markdown extensiosn now individually packaged

Simplified get_markdown function
parent f0cafb9c
......@@ -22,6 +22,12 @@ Please first install these software in order to get AA running properly:
- Python dateutil <http://labix.org/python-dateutil>
- Pygraphviz <http://networkx.lanl.gov/pygraphviz/>
- Python librdf (Redland) <http://librdf.org/>
- Python mdx cite <http://pypi.python.org/pypi/mdx_cite>
- Python mdx del ins <http://pypi.python.org/pypi/mdx_del_ins>
- Python mdx semantic wikilinks <http://pypi.python.org/pypi/mdx_semanticwikilinks>
- Python mdx semantic data <http://pypi.python.org/pypi/mdx_semanticdata>
- Python mdx outline <http://pypi.python.org/pypi/mdx_outline>
On Ubuntu, this should do the trick:
......@@ -33,6 +39,11 @@ On Ubuntu, this should do the trick:
sudo pip install diff-match-patch
sudo pip install python-dateutil
sudo pip install pygraphviz
sudo pip install mdx_cite
sudo pip install mdx_del_ins
sudo pip install mdx_semanticwikilinks
sudo pip install mdx_semanticdata
sudo pip install mdx_outline
sudo apt-get install python-librdf
Additionally, you'll need the following for the RDF sniffers:
......
......@@ -113,3 +113,9 @@ Meeting 09/03/2012
- whitespace metadata
- fixing default metadata + doc
Meeting Skyope long-court 27/04/2012
====================================
- Scroll personalisé
......@@ -14,29 +14,19 @@ Requires Python-Markdown 2.0+.
import markdown
try: from markdown.util import etree
except ImportError: from markdown import etree
import mdx_semanticwikilinks
import mdx_semanticdata
import mdx_sectionedit
import mdx_addsections
import mdx_timecodes
import mdx_del_ins
import mdx_cite
#import mdx_cut
from aawiki.utils import url_for_pagename
import urlparse
def make_link(rel, target, label, default_link_rel=None, filter_=None):
"""
Custom implementation of the SemanticWikilinks make_link function.
Returns ElementTree Element.
"""
def aa_make_link(rel, target, label):
a = etree.Element('a')
#import pdb; pdb.set_trace();
parts = urlparse.urlparse(target)
# FIXME: This was added to give anchor support within a wiki page.
# Many link styles are not taken into account yet.
parts = urlparse.urlparse(target)
if parts.fragment and not (parts.scheme or parts.netloc):
href = "#%s" % parts.fragment
elif parts.scheme:
......@@ -45,29 +35,13 @@ def make_link(rel, target, label, default_link_rel=None, filter_=None):
else:
href = url_for_pagename(target)
a.set('href', href)
if filter_:
a.set('data-filter', filter_)
if rel == "aa:embed":
if rel and rel == "aa:embed":
img = etree.Element('img')
img.set('src', '/static/aacore/img/ajax-loader.gif')
img.set('src', '/static/aawiki/img/ajax-loader.gif')
a.append(img)
else:
a.text = label or target
if rel:
a.set('rel', rel)
elif default_link_rel:
a.set('rel', default_link_rel)
return a
def aa_make_link(rel, target, label):
a = etree.Element('a')
a.set('href', target)
if rel:
a.set('rel', rel or 'aa:link')
else:
a.set('rel', 'aa:link')
a.set('rel', rel or 'aa:link')
if label:
if label.startswith('|'):
......@@ -84,100 +58,20 @@ def aa_make_link(rel, target, label):
return a
#"""
# <a rel="aa:link" href="/pages/Anthology_walk%2Btalk_Brussels">
# <span about="/pages/Anthology_walk%2Btalk_Brussels">
# <span property="aa:linklabel">this anthology</span>
# <span property="aa:linktarget" content="Anthology walk+talk Brussels"></span>
# </span>
# </a>
#"""
#def make_link (rel, target, label, default_link_rel=None):
# """ More semantically complete version, asserts linktarget and label (if different) via nested spans. """
# a = markdown.etree.Element('a')
# href = url_for_pagename(target)
# a.set('href', href)
# if rel:
# a.set('rel', rel)
# elif default_link_rel:
# a.set('rel', default_link_rel)
# label = label or target
# span = markdown.etree.Element('span')
# span.set('about', href)
# a.append(span)
# if label != target:
# label_span = markdown.etree.Element('span')
# label_span.set("property", "aa:linklabel")
# label_span.text = label
# span.append(label_span)
# target_span = markdown.etree.Element('span')
# target_span.set("property", "aa:linktarget")
# target_span.set("content", target)
# span.append(target_span)
# else:
# # Only include aa:linktarget
# target_span = markdown.etree.Element('span')
# target_span.set("property", "aa:linktarget")
# target_span.text = target
# span.append(target_span)
# return a
def get_markdown(default_link_rel="aa:link"):
def get_markdown(simple=False):
"""
This is a function to return a Active Archive markdown instance.
Returns a Markdown instance.
Returns a Markdown instance with the appropriate extensions for active
archives wiki.
"""
return markdown.Markdown(output_format="html5", extensions=[
"headerid",
"attr_list",
#"abbr",
"def_list",
"fenced_code",
"footnotes",
#"tables"
"meta",
mdx_semanticwikilinks.makeExtension(configs=[
('make_link', make_link),
('default_link_rel', default_link_rel)]),
#mdx_cut.makeExtension(),
mdx_semanticdata.makeExtension(),
mdx_timecodes.makeExtension(),
mdx_del_ins.makeExtension(),
mdx_cite.makeExtension(),
mdx_addsections.makeExtension(configs=[('class', 'section%(LEVEL)d')]),
],
)
def get_simple_markdown(default_link_rel="aa:link"):
"""
This is a function to return a Active Archive markdown instance.
Returns a Markdown instance.
"""
return markdown.Markdown(output_format="html5", extensions=[
"attr_list",
#"abbr",
"def_list",
"fenced_code",
#"footnotes",
#"tables"
#"meta",
mdx_semanticwikilinks.makeExtension(configs=[
('make_link', make_link),
('default_link_rel', default_link_rel)]),
#mdx_cut.makeExtension(),
mdx_semanticdata.makeExtension(),
#mdx_timecodes.makeExtension(),
mdx_del_ins.makeExtension(),
mdx_cite.makeExtension(),
#mdx_addsections.makeExtension(configs=[('class', 'section%(LEVEL)d')]),
],
)
extension_configs = {'semanticwikilinks': [('make_link', aa_make_link)]}
extensions = ["attr_list", "cite", "def_list", "del_ins", "semanticdata",
"semanticwikilinks", mdx_timecodes.makeExtension()]
if not simple:
extensions.extend(["footnotes", "headerid", "meta", "outline"])
return markdown.Markdown(output_format="html5", extensions=extensions,
extension_configs=extension_configs)
if __name__ == "__main__":
......
#! /usr/bin/env python
'''
AddSections Extension for Python-Markdown
=========================================
Wraps Document in <section> tags based on a hierarchy of header tags.
By default, adds a class = "sectionN" where N is the header level being
wrapped.
Requires Python-Markdown 2.0+.
Basic usage:
>>> import markdown
>>> src = """
... # 1
... Section 1
... ## 1.1
... Subsection 1.1
... ## 1.2
... Subsection 1.2
... ### 1.2.1
... Hey 1.2.1 Special section
... ### 1.2.2
... #### 1.2.2.1
... # 2
... Section 2
... """.strip()
>>> html = markdown.markdown(src, ['addsections'])
>>> print(html)
<section class="section1"><h1>1</h1>
<p>Section 1</p>
<section class="section2"><h2>1.1</h2>
<p>Subsection 1.1</p>
</section><section class="section2"><h2>1.2</h2>
<p>Subsection 1.2</p>
<section class="section3"><h3>1.2.1</h3>
<p>Hey 1.2.1 Special section</p>
</section><section class="section3"><h3>1.2.2</h3>
<section class="section4"><h4>1.2.2.1</h4>
</section></section></section></section><section class="section1"><h1>2</h1>
<p>Section 2</p>
</section>
Divs instead of sections, custom class names:
>>> src = """
... # Introduction
... # Body
... ## Subsection
... # Bibliography
... """.strip()
>>> html = markdown.markdown(src, extensions=['addsections(tag=div,class=s%(LEVEL)d)'])
>>> print(html)
<div class="s1"><h1>Introduction</h1>
</div><div class="s1"><h1>Body</h1>
<div class="s2"><h2>Subsection</h2>
</div></div><div class="s1"><h1>Bibliography</h1>
</div>
Typeof attribute:
>>> src = """
... # Introduction
... # Body
... ## Subsection
... # Bibliography
... """.strip()
>>> html = markdown.markdown(src, extensions=['addsections(typeof=aa:annotation)'])
>>> print(html)
<section class="section1" typeof="aa:annotation"><h1>Introduction</h1>
</section><section class="section1" typeof="aa:annotation"><h1>Body</h1>
<section class="section2" typeof="aa:annotation"><h2>Subsection</h2>
</section></section><section class="section1" typeof="aa:annotation"><h1>Bibliography</h1>
</section>
FIXME: Known Issue: structures like this one produces confusing results
(the ## gets placed inside the ###)
>>> src="""
... # ONE
... ### TOO Deep
... ## Level 2
... # TWO
... """.strip()
>>> html = markdown.markdown(src, extensions=['addsections'])
>>> print(html)
<section class="section1"><h1>ONE</h1>
<section class="section3"><h3>TOO Deep</h3></section>
<section class="section2"><h2>Level 2</h2>
</section></section><section class="section1"><h1>TWO</h1>
</section>
'''
import markdown, re
from markdown.util import etree
def add_sections(tree, tag, tagclass, typeof, moveAttributes=True):
def do(parent, n, tag, tagclass):
tagname = "h%d" % n
wrapper = None
children = list(parent)
for i, child in enumerate(children):
# should allow lower level to stop as well
m = re.search(r"h(\d+)", child.tag)
if m:
tag_level = int(m.group(1))
if m and tag_level == n: # child.tag == tagname:
# FOUND HEADER: START NEW WRAP
wrapper = etree.Element(tag)
if moveAttributes:
for key, value in child.attrib.items():
wrapper.set(key, value)
del child.attrib[key]
if typeof:
wrapper.set("typeof", typeof)
if tagclass:
classes = wrapper.get("class", "")
if '%(LEVEL)d' in tagclass:
tagclass = tagclass % {'LEVEL': n}
wrapper.set("class", " ".join([tagclass, classes]).strip())
parent.remove(child)
parent.insert(i, wrapper)
wrapper.append(child)
elif wrapper:
# ADD SIBLING TO CURRENT WRAP
parent.remove(child)
wrapper.append(child)
else:
# RECURSE
do(child, n, tag, tagclass)
for i in range(1, 7):
do(tree, i, tag, tagclass)
class AddSectionsTreeprocessor(markdown.treeprocessors.Treeprocessor):
def run(self, doc):
add_sections(doc, self.config.get("tag")[0], self.config.get("class")[0],
self.config.get("typeof")[0])
class AddSectionsExtension(markdown.Extension):
def __init__(self, configs):
self.config = {
'tag': ['section', 'tag name to use, default: section'],
'class': ['section%(LEVEL)d', 'class name, may include %(LEVEL)d to reference header-level (i.e. h1, h2)'],
'typeof': ['', 'sets typeof attribute for rdfa']
}
for key, value in configs:
self.setConfig(key, value)
def extendMarkdown(self, md, md_globals):
ext = AddSectionsTreeprocessor(md)
ext.config = self.config
md.treeprocessors.add("addsections", ext, "_end")
def makeExtension(configs={}):
return AddSectionsExtension(configs=configs)
if __name__ == "__main__":
import doctest
doctest.testmod()
#! /usr/bin/env python
'''
Cite Extension for Python-Markdown
==================================
Wraps the inline content into <cite> tags.
Tested with Python-Markdown 2.0+.
Basic usage:
>>> import markdown
>>> src = '"""Who Is Killing the Great Chefs of Europe?""" is the last movie I watched.'
>>> html = markdown.markdown(src, ['cite'])
>>> print(html)
<p><cite>Who Is Killing the Great Chefs of Europe?</cite> is the last movie I watched.</p>
'''
import re
import markdown
from markdown.inlinepatterns import SimpleTagPattern
CITE_RE = r'(\"{3})(.+?)\2'
class CiteExtension(markdown.extensions.Extension):
""" Add smart_emphasis extension to Markdown class."""
def extendMarkdown(self, md, md_globals):
""" Modify inline patterns. """
#md.inlinePatterns.add('cite', SimpleTagPattern(CITE_RE, 'cite'), '>emphasis2')
md.inlinePatterns.add('cite', SimpleTagPattern(CITE_RE, 'cite'), '<not_strong')
def makeExtension(configs={}):
return CiteExtension(configs=dict(configs))
if __name__ == "__main__":
import doctest
doctest.testmod()
#! /usr/bin/env python
'''
Del/Ins Extension for Python-Markdown
=====================================
Wraps the inline content in ins/del tags.
Tested with Python-Markdown 2.0+.
Basic usage:
>>> import markdown
>>> src = """This is ++added content++ and this is ~~deleted content~~"""
>>> html = markdown.markdown(src, ['del_ins'])
>>> print(html)
<p>This is <ins>added content</ins> and this is <del>deleted content</del>
</p>
'''
import re
import markdown
from markdown.inlinepatterns import SimpleTagPattern
DEL_RE = r"(\~\~)(.+?)(\~\~)"
INS_RE = r"(\+\+)(.+?)(\+\+)"
class DelInsExtension(markdown.extensions.Extension):
""" Add smart_emphasis extension to Markdown class."""
def extendMarkdown(self, md, md_globals):
""" Modify inline patterns. """
md.inlinePatterns.add('del', SimpleTagPattern(DEL_RE, 'del'), '<not_strong')
md.inlinePatterns.add('ins', SimpleTagPattern(INS_RE, 'ins'), '<not_strong')
def makeExtension(configs={}):
return DelInsExtension(configs=dict(configs))
if __name__ == "__main__":
import doctest
doctest.testmod()
#!/usr/bin/env python
'''
SemanticData Extension for Python-Markdown
======================================
Converts %%property :: value | display%%, where display is optional.
Customizable with make_metadata option as to what the actual element is.
Requires Python-Markdown 2.0+.
Basic usage:
>>> import markdown
>>> text = "%%dc:author :: Sherry Turkle | Turkle's%% %%dc:title::Second Self%% was an early book on the social aspects of computation."
>>> html = markdown.markdown(text, ['semanticdata'])
>>> print(html)
<p><span content="Sherry Turkle" property="dc:author">Turkle's</span> <span content="Second Self" property="dc:title">Second Self</span> was an early book on the social aspects of computation.</p>
'''
import markdown
import re
#pattern = r"""
#\%\%\s*
# (?:(?P<property>[^\]#]+?) \s* ::) \s*
# (?P<value>.+?) \s*
# (?:\| \s* (?P<display>[^\]]+?) \s*)?
#\%\%
#""".strip()
pattern = r"""
\%\%\s*
(?:((?P<namespace>\w+):)?(?P<rel>[^\%#]+?) \s* ::)? \s*
(?P<target>.+?) \s*
(?:\| \s* (?P<label>[^\]]+?) \s*)?
\%\%
""".strip()
def make_elt (rel, target, label):
elt = markdown.util.etree.Element('span')
elt.set('content', target)
elt.text = label or target
if rel:
elt.set('property', rel)
elt.set('title', "%s::%s" % (rel, target))
return elt
class SemanticDataExtension(markdown.Extension):
def __init__(self, configs):
self.config = {
'make_elt' : [make_elt, 'Callback to convert parts into an HTML/etree element (default <span>)'],
'namespace' : ['aa', 'Default namespace'],
}
# Override defaults with user settings
for key, value in configs :
self.setConfig(key, value)
def extendMarkdown(self, md, md_globals):
self.md = md
# append to end of inline patterns
pat = SemanticDataPattern(self.config, md)
md.inlinePatterns.add('semanticdata', pat, "<not_strong")
class SemanticDataPattern(markdown.inlinepatterns.Pattern):
def __init__(self, config, md=None):
markdown.inlinepatterns.Pattern.__init__(self, '', md)
# self.markdown = md # done by super
self.compiled_re = re.compile("^(.*?)%s(.*?)$" % pattern, re.DOTALL | re.X)
self.config = config
def getCompiledRegExp (self):
return self.compiled_re
def handleMatch(self, m):
""" Returns etree """
d = m.groupdict()
fn = self.config['make_elt'][0]
namespace = d.get("namespace") or self.config['namespace'][0]
rel = d.get("rel")
if rel:
rel = "%s:%s" % (namespace, d.get("rel"))
return fn(rel, d.get("target"), d.get("label"))
def makeExtension(configs={}) :
return SemanticDataExtension(configs=configs)
if __name__ == "__main__":
import doctest
doctest.testmod()
#! /usr/bin/env python
'''
SemanticWikiLinks Extension for Python-Markdown
===============================================
Converts links of style [[rel :: target | label || pipeline ]], where rel,
label and pipeline are optional. Customizable with make_link option as to what
the actual element is.
Requires Python-Markdown 2.0+.
Basic usage:
>>> text = "Some text with a [[WikiLink]]."
>>> html = markdown.markdown(text, ['semanticwikilinks'])
>>> print(html)
<p>Some text with a <a href="WikiLink">WikiLink</a>.</p>
>>> text = "[[http://activearchives.org/]], [[#id|anchor]], [[../index.html|a relative link]], [[/|an absolute link]], [[/index.html|another absolute link]]"
>>> html = markdown.markdown(text, ['semanticwikilinks'])
>>> print(html)
<p>Some text with a <a href="http://activearchives.org/">http://activearchives.org/</a>.</p>
Define a custom URL builder:
>>> def make_rdfa(rel, target, label, default_link_rel=None, filter_=None):
... elt = etree.Element("span")
... elt.set("property", rel)
... elt.set("value", target)
... elt.text = label or target
... return elt
>>> md = markdown.Markdown(extensions=['semanticwikilinks'],
... extension_configs={'semanticwikilinks' : [('make_link', make_rdfa)]})
>>> html = md.convert('[[ Speaker :: Sherry Turkle | Second Self ]]')
>>> print(html)
<p><span property="aa:Speaker" value="Sherry Turkle">Second Self</span></p>
Change the default namespace ("aa"):
>>> md = markdown.Markdown(extensions=['semanticwikilinks'],
... extension_configs={'semanticwikilinks' : [('namespace', 'mynamespace')]})
>>> html = md.convert('[[ Speaker :: Sherry Turkle | Second Self ]]')
>>> print(html)
<p><a href="Sherry Turkle" rel="mynamespace:Speaker">Second Self</a></p>