Skip to content
Find file
70 lines (57 sloc) 3.21 KB
# -*- coding: utf8 -*-
import time
start_time = time.time()
# get the parser
from pijnu import makeParser
preprocessorGrammar = file("preprocessor.pijnu").read()
mediawikiGrammar = file("mediawiki.pijnu").read()
allowed_tags = ['p', 'span', 'b', 'i', 'small', 'center']
allowed_autoclose_tags = ['br', 'hr']
allowed_parameters = ['class', 'style', 'name', 'id', 'scope']
interwiki = {'ar': '',
'az': '',
'br': '',
'ca': '',
'cs': '',
'da': '',
'de': '',
'en': '',
'eo': '',
'es': '',
'fr': ''}
namespaces = {'Template': 10,
u'Catégorie': 14,
'Category': 14,
'File': 6,
'Fichier': 6,
'Image': 6}
templates = {'listen': u"""{| style="text-align:center; background: #f9f9f9; color: #000;font-size:90%; line-height:1.1em; float:right;clear:right; margin:1em 1.5em 1em 1em; width:300px; border: 1px solid #aaa; padding: 0.1em;" cellspacing="7"
! class="media audio" style="background-color:#ccf; line-height:3.1em" | Fichier audio
|<span style="height:20px; width:100%; padding:4pt; padding-left:0.3em; line-height:2em;" cellspacing="0">'''[[Media:{{{filename|{{{nomfichier|{{{2|}}}}}}}}}|{{{title|{{{titre|{{{1|}}}}}}}}}]]''' ''([[:Fichier:{{{filename|{{{nomfichier|{{{2|}}}}}}}}}|info]])''<br /><small>{{{suitetexte|{{{description|}}}}}}</small>
<center>[[Fichier:{{{filename|{{{nomfichier|{{{2|}}}}}}}}}|noicon]]</center></span><br /><span style="height:20px; width:100%; padding-left:0.3em;" cellspacing="0"><span title="Des difficultés pour écouter le fichier ?">[[Image:Circle question mark.png|14px|link=Aide:Écouter des sons ogg|Des difficultés pour écouter le fichier ?]] ''[[Aide:Écouter des sons ogg|Des problèmes pour écouter le fichier ?]]''</span>
'3e': '3<sup>e</sup>'}
from preprocessor import make_parser
preprocessor = make_parser(templates)
from html import make_parser
parser = make_parser(allowed_tags, allowed_autoclose_tags, allowed_parameters, interwiki, namespaces)
# import the source in a utf-8 string
import codecs
fileObj ="wikitext.txt", "r", "utf-8")
source =
# The last line of the file will not be parsed correctly if
# there is no newline at the end of file, so, we add one.
if source[-1] != '\n':
source += '\n'
preprocessed_text = preprocessor.parse(source)
tree = parser.parse(preprocessed_text.leaves())
output = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
<html xmlns="" xml:lang="fr">
<head><title>Test!</title></head>""" + tree.leaves() + "</html>"
file("article.htm", "w").write(output.encode('UTF-8'))
end_time = time.time()
print "Parsed and rendered in", end_time - start_time, "s."
Jump to Line
Something went wrong with that request. Please try again.