#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2008,2015 Sergey Poznyakoff
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
' + self.format(elt['content']) + '' elif elt['tag'] == 'code': self.nested += 1 s = self.format(elt['content']) self.nested -= 1 return '
' + s + '
' #FIXME
else:
s = '<' + elt['tag']
if elt['args']:
s += ' ' + elt['args']
s += '>'
s += self.format(elt['content'])
return s + '' + elt['tag'] + '>'
def str_para(self, elt):
string = "";
for x in elt['content']:
string += self.format(x)
return "" + string + "
" def str_pre(self, elt): string = ""; for x in elt['content']: string += self.format(x) if self.nested: return string return '' + string + '' def str_ind(self, elt): return (" " * 2 * elt['level']) + self.format(elt['content']) def format(self, elt): if elt['type'] == 'TEXT': if isinstance(elt['content'],list): string = "" for s in elt['content']: string += s else: string = elt['content'] return string elif elt['type'] == 'TAG': return self.str_tag(elt) elif elt['type'] == 'PARA': return self.str_para(elt) elif elt['type'] == 'PRE': return self.str_pre(elt) elif elt['type'] == 'IT': return self.str_it(elt) elif elt['type'] == 'BOLD': return self.str_bold(elt) elif elt['type'] == 'LINK': return self.str_link(elt) elif elt['type'] == 'TMPL': return self.str_tmpl(elt) elif elt['type'] == 'BAR': return self.str_bar() elif elt['type'] == 'HDR': return self.str_hdr(elt) elif elt['type'] == 'REF': return self.str_ref(elt) elif elt['type'] == 'ENV': return self.str_env(elt) elif elt['type'] == 'IND': return self.str_ind(elt) elif elt['type'] == 'SEQ': string = "" for x in elt['content']: string += self.format(x) return string else: return str(elt) def __str__(self): str = "" for elt in self.tree: str += self.format(elt) return str class HtmlWiktionaryMarkup (HtmlWikiMarkup): """ A class for translating Wiktionary articles into HTML. This version does not do much, except that it tries to correctly format templates. But "tries" does not mean "does". The heuristics used here is clearly not enough to cope with it. 1. FIXME: The right solution would be to have a database of templates with their semantics and to decide on their rendering depending on that. E.g. {{term}} in en.wiktionary means "replace this with the search term". This, however, does not work in other wiktionaries. There are also more complex templates, e.g.: {{t+|bg|врата|n|p|tr=vrata|sc=Cyrl}} I don't know what it means. Couldn't find any documentation either. Again, this template does not work in other dictionaries. 2. Capitulation notice: Given the: 1. waste amount of wiktionaries available, 2. abundance of various templates for each wictionary, 3. apparent lack of documentation thereof, 4. the lack of standardized language-independent templates, I dont see any way to cope with the template-rendering task within a reasonable amount of time. Faeci quod potui, faciant meliora potentes. """