def parseXML(self, file_xml, folder_conteudo, export_version): """Parse XML. https://github.com/zikzakmedia/python-mediawiki """ context = aq_inner(self.context) utils = getToolByName(context, 'plone_utils') NS = '{http://www.mediawiki.org/xml/export-' + export_version + '/}' conteudo = [] with open(file_xml.name) as f: for event, elem in iterparse(f): if elem.tag == '{0}page'.format(NS): title = elem.find("{0}title".format(NS)) contr = elem.find(".//{0}username".format(NS)) text = elem.find(".//{0}text".format(NS)) if (title is not None) and (contr is not None) and (text is not None): text = unicode(text.text).encode('utf-8') text = wiki2html(text, True) conteudo.append(dict(title=title.text, contr=contr.text, text=text)) elem.clear() self.createDocument(conteudo, folder_conteudo) msg = 'Procedimento executado.' utils.addPortalMessage(msg, type='info')
def content_html(self): if self.markup == 'xhtml': return self.content elif self.markup == 'mediawiki': return mediawiki.wiki2html(self.content, True) else: return markdown2.markdown(self.content)
def mediawiki2markdown(source): try: from mediawiki import wiki2html except ImportError: raise ImportError('GPL library "mediawiki" from https://github.com/zikzakmedia/python-mediawiki.git ' 'is required for this operation') wiki_content = wiki2html(source, True) wiki_content = _convert_toc(wiki_content) markdown_text = html2text.html2text(wiki_content) markdown_text = markdown_text.replace('<', '<').replace('>', '>') return markdown_text
def POST(self, content=""): try: if privilege() < 1: raise Exception("privilege is error") datadict = dict(urlparse.parse_qsl(web.data())) data = datadict["data"] if len(data)==0: raise Exception("content is empty!") return wikiparser.wiki2html(data, False) except Exception, err: web.BadRequest() return '{"desc": "%s"}' % (err)
def mediawiki2markdown(source): try: from mediawiki import wiki2html except ImportError: raise ImportError( 'GPL library "mediawiki" from https://github.com/zikzakmedia/python-mediawiki.git ' 'is required for this operation') wiki_content = wiki2html(source, True) wiki_content = _convert_toc(wiki_content) markdown_text = html2text.html2text(wiki_content) markdown_text = markdown_text.replace('<', '<').replace('>', '>') return markdown_text
def mediawiki2markdown(source): try: import html2text from mediawiki import wiki2html except ImportError: raise ImportError("""This operation requires GPL libraries: "mediawiki" (https://github.com/zikzakmedia/python-mediawiki.git) "html2text" (https://github.com/aaronsw/html2text.git)""") html2text.BODY_WIDTH = 0 wiki_content = wiki2html(source, True) wiki_content = _convert_toc(wiki_content) markdown_text = html2text.html2text(wiki_content) markdown_text = markdown_text.replace('<', '<').replace('>', '>') return markdown_text
def mediawiki2markdown(source): try: import html2text from mediawiki import wiki2html except ImportError: raise ImportError("""This operation requires GPL libraries: "mediawiki" (https://pypi.org/project/mediawiki2html/) "html2text" (https://pypi.org/project/html2text/)""") html2text.BODY_WIDTH = 0 wiki_content = wiki2html(source, True) wiki_content = _convert_toc(wiki_content) markdown_text = html2text.html2text(wiki_content) markdown_text = markdown_text.replace('<', '<').replace('>', '>') return markdown_text
def GET(self, blogid=None, isrenderwiki=False): try: query = ("select blog.blog_id as blog_id, blog.title as title, " \ "blog.summary as summary, blog.published as published, " \ "blog.updated as updated, blog.content as content, " \ "blog.privilege as privilege, " \ "category_link.category_id as category_id " \ "from blog left outer join category_link " \ "on blog.blog_id = category_link.blog_id " \ "where blog.blog_id='%s' and " \ "blog.privilege <= %d;") % (blogid, privilege()) curlist = db.query(query).list() if len(curlist) != 1: raise Exception("find blog is not one.[%d]" % (len(curlist))) item = curlist[0] bloglist = [] blogitem = {} blogitem["blogid"] = item["blog_id"] blogitem["title"] = item["title"] if isrenderwiki and item["published"] > neweditstyle: #blogitem["summary"] = wiki.wiki2html(item["summary"], False) first = Code2Html() result = first.convertBeforeWiki(item["content"]) result = wiki.wiki2html(result, False) blogitem["content"] = first.convertAfterWiki(result) else: #blogitem["summary"] = item["summary"] blogitem["content"] = item["content"] blogitem["published"] = time.strftime("%Y-%m-%d %H:%M:%S" , time.localtime(item["published"])) blogitem["updated"] = time.strftime("%Y-%m-%d %H:%M:%S" , time.localtime(item["updated"])) blogitem["privilege"] = item["privilege"] blogitem["category_id"] = 0 if item["category_id"] is not None: blogitem["category_id"] = item["category_id"] bloglist.append(blogitem) return json.dumps(bloglist, ensure_ascii=False) except Exception, err: web.BadRequest() return '{"desc": "%s"}' % (err)
def __all__(self, categoryid, pageindex=1, count=8): try: offset = int(count) start = (int(pageindex) - 1) * offset query = "select blog.blog_id as blog_id, blog.title as title, " \ "blog.summary as summary, blog.published as published, " \ "blog.updated as updated, " \ "category_link.category_id as category_id " \ "from blog left outer join " \ "category_link on category_link.blog_id = blog.blog_id " \ "where blog.privilege <= %d order by published desc " \ "limit %d, %d;" % (privilege(), start, offset) curlist = db.query(query).list() bloglist = [] for item in curlist: blogitem = {} blogitem["blogid"] = item["blog_id"] blogitem["title"] = item["title"] if item["published"] > neweditstyle: first = Code2Html() result = first.convertBeforeWiki(item["summary"]) result = wiki.wiki2html(result, False) blogitem["summary"] = first.convertAfterWiki(result) else: blogitem["summary"] = item["summary"] blogitem["published"] = time.strftime("%Y-%m-%d %H:%M:%S" , time.localtime(item["published"])) blogitem["updated"] = time.strftime("%Y-%m-%d %H:%M:%S" , time.localtime(item["updated"])) blogitem["category_id"] = 0 if item["category_id"] is not None: blogitem["category_id"] = item["category_id"] bloglist.append(blogitem) return json.dumps(bloglist, ensure_ascii=False) except Exception, err: web.BadRequest() return '{"desc": "%s"}' % (err)
def __gettype__(self, categoryid, pageindex=1, count=8): try: offset = int(count) start = (int(pageindex) - 1) * offset curwhere = "blog.privilege <= %d and category_link.category_id=%s " \ "and category_link.blog_id=blog.blog_id" % (privilege(), categoryid) what="blog.blog_id as blog_id, blog.title as title, " \ "blog.summary as summary, blog.published as published, " \ "blog.updated as updated" order = "published desc" curlist = db.select("blog, category_link", what=what, where=curwhere, order=order, limit="%d, %d" % (start,offset)).list() bloglist = [] categoryid = int(categoryid) for item in curlist: blogitem = {} blogitem["blogid"] = item["blog_id"] blogitem["title"] = item["title"] if item["published"] > neweditstyle: first = Code2Html() result = first.convertBeforeWiki(item["summary"]) result = wiki.wiki2html(result, False) blogitem["summary"] = first.convertAfterWiki(result) else: blogitem["summary"] = item["summary"] blogitem["published"] = time.strftime("%Y-%m-%d %H:%M:%S" , time.localtime(item["published"])) blogitem["updated"] = time.strftime("%Y-%m-%d %H:%M:%S" , time.localtime(item["updated"])) blogitem["category_id"] = categoryid bloglist.append(blogitem) return json.dumps(bloglist, ensure_ascii=False) except Exception, err: web.BadRequest() return '{"desc": "%s"}' % (err)
def gen_html(self): return mediawiki.wiki2html(self.text, True)
def html(self): return mediawiki.wiki2html(self.text or '', False)
def _text_wiki(self, text): return wiki2html(text, True)
def html(self, input_text): import mediawiki return mediawiki.wiki2html(input_text, False)
def mediawiki(env, value): output = value d = {} return wiki2html(output, False)