示例#1
0
    def __parse(self, fragment):
        content = KnowledgeParser.fetch_content(fragment)
        if content is None:
            return None
        else:
            self.id = content["id"]
            self.title = content["title"]
            self.wikitext = content["wikitext"]

        categorylinks = self.fetch_categorylinks()

        # remove the first pic and format the remaining pics
        allpics = self.__remove_first_pic()

        # remove category from wikitext
        self.__remove_categories()

        # wikitext -> html
        self.wikitext = self.wikitext.strip()
        body_html = parselite(self.wikitext)

        output = {
            "pic": allpics[0] if allpics else None,
            "title": self.title,
            "body": body_html,
            "categorylinks": categorylinks
        }

        return output
示例#2
0
文件: views.py 项目: xeeer/jieblog
def wiki(request):
	user = users.get_current_user()
	if user:
		greeting = ("<a href=\"%s\">Sign Out</a>" %
				  (users.create_logout_url("/")))			
	else:
		greeting = ("<a href=\"%s\">Sign in</a>" %
				  users.create_login_url("/"))
	if users.is_current_user_admin():
		show_edit = True
	else:
		show_edit = False
	wikiz = models.Wiki.all()
	for wiki in wikiz:
		wiki_text=parselite(wiki.content)
	payload = dict(wiki_text=wiki_text)
	return render('wiki.html', payload)
示例#3
0
文件: markup.py 项目: brezerk/taverna
def markup(value, parser):
    esc = conditional_escape
    if parser == 1:
        import postmarkup
        from pygments import highlight
        from pygments.lexers import get_lexer_by_name, ClassNotFound
        #from pygments.lexers import guess_lexer
        from pygments.formatters import HtmlFormatter


        markup = postmarkup.create(annotate_links=False,exclude=["img", "code"],use_pygments=False)

        class ImgTag(postmarkup.TagBase):
            valid_params = ("left", "right")

            def __init__(self, name, **kwargs):
                postmarkup.TagBase.__init__(self, name, inline=True)

            def render_open(self, parser, node_index):
                contents = self.get_contents(parser)
                self.skip_contents(parser)
                contents = postmarkup.strip_bbcode(contents).replace(u'"', "%22")

                if self.params in self.valid_params:
                    return u'<img class="float-%s" src="%s" alt="%s">' % (self.params, contents, contents)
                else:
                    return u'<img src="%s" alt="%s">' % (contents, contents)

        class PygmentsCodeTag(postmarkup.TagBase):
            def __init__(self, name, pygments_line_numbers=True, **kwargs):
                postmarkup.TagBase.__init__(self, name, enclosed=True, strip_first_newline=True)
                self.line_numbers = pygments_line_numbers

            def render_open(self, parser, node_index):
                contents = self.get_contents(parser)
                self.skip_contents(parser)

                #if self.params:
                try:
                    lexer = get_lexer_by_name(self.params, stripall=True)
                except ClassNotFound:
                    contents = postmarkup._escape_no_breaks(contents)
                    return '<div class="code"><pre>%s</pre></div>' % contents
                #Well, do we realy need lexer gues?
                #else:
                #    lexer = guess_lexer(contents)

                formatter = HtmlFormatter(linenos='inline', cssclass="code")
                return highlight(contents, lexer, formatter)

        markup.add_tag(ImgTag, u'img')
        markup.add_tag(PygmentsCodeTag, u'code')
        markup.add_tag(postmarkup.SimpleTag, u'block', u'div class="mblock"')

        value = "<p>" + markup(value) + "</p>"
        return value
    elif parser == 2:
        from markdown import markdown
        value = esc(value)
        value = markdown(value)
        return value
    elif parser == 3:
        from wikimarkup import parselite
        value = parselite(value)
    elif parser == 4:
        from django.template.defaultfilters import removetags
        value = removetags(value, 'style html script applet form frame iframe map noframes noscript object var area input button select')
        value = linebreaks(value)
    else:
        value = esc(value)
        value = urlize(value)
        value = linebreaks(value)
    return value
示例#4
0
import json, re

import requests
from wikimarkup import parselite

def fetch_wikitext(url):
	r = requests.get(url)
	decode_json = json.loads(r.text)
	wikitext = decode_json["query"]["pages"]["2"]["revisions"][0]["*"]
	return wikitext

if __name__ == "__main__":
	wikitext = fetch_wikitext("http://localhost/mediawiki/api.php?action=query&prop=revisions&rvprop=content&titles=HelloWorld&format=json")
	print "======format: wikitext====="
	print wikitext
	print "======format: html========="
	print parselite(wikitext)

	picRegex = re.compile(r'[[File:(*) | *]]')
	matchObj = picRegex.search(wikitext)
	print matchObj.group(1)