summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2018-08-05 08:09:01 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-08-05 08:37:49 (GMT)
commita4917480cc989ce2cbaee79345149a7be22eccac (patch) (side-by-side diff)
treed1a1bd895e7d305cd894941ea37faebad039c354
parent1fb7bc5aefaebf830b969c418f7987f50f4187f7 (diff)
downloadwikitrans-a4917480cc989ce2cbaee79345149a7be22eccac.tar.gz
wikitrans-a4917480cc989ce2cbaee79345149a7be22eccac.tar.bz2
Implement <ref> and <references>
* WikiTrans/wikimarkup.py (BaseWikiMarkup.tags): Add 'ref' and 'references'. * WikiTrans/wikitoken.py (WikiTagNode): If the parser has the 'references' attribute, store each <ref> there. * WikiTrans/wikidump.py: Handle new tags. * WikiTrans/wiki2html.py (HtmlTagNode): Handle new tags. (HtmlWikiMarkup.references): New attribute * WikiTrans/wiki2texi.py (TexiTagNode): Handle new tags. * WikiTrans/wiki2text.py (TextTagNode): Handle new tags. (TextWikiMarkup.showrefs): Renamed from references (TextWikiMarkup.references): New attribute.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--WikiTrans/wiki2html.py21
-rw-r--r--WikiTrans/wiki2texi.py7
-rw-r--r--WikiTrans/wiki2text.py17
-rw-r--r--WikiTrans/wikidump.py8
-rw-r--r--WikiTrans/wikimarkup.py2
-rw-r--r--WikiTrans/wikitoken.py11
6 files changed, 59 insertions, 7 deletions
diff --git a/WikiTrans/wiki2html.py b/WikiTrans/wiki2html.py
index 81b918f..7c71602 100644
--- a/WikiTrans/wiki2html.py
+++ b/WikiTrans/wiki2html.py
@@ -143,6 +143,25 @@ class HtmlTagNode(WikiTagNode):
s = self.content.format()
self.parser.nested -= 1
return '<pre><code>' + s + '</code></pre>' #FIXME
+ elif self.tag == 'ref':
+ n = self.idx+1
+ return '<sup id="cite_ref-%d" class="reference"><a name="cite_ref-%d" href=#cite_note-%d">%d</a></sup>' % (n,n,n,n)
+ elif self.tag == 'references':
+ s = '<div class="references">\n'
+ s += '<ol class="references">\n'
+ n = 0
+ for ref in self.parser.references:
+ n += 1
+ s += ('<li id="cite_note-%d">' + \
+ '<span class="mw-cite-backlink">' + \
+ '<b><a href="#cite_ref-%d">^</a></b>' + \
+ '</span>' + \
+ '<span class="reference-text">' + \
+ ref.content.format() + \
+ '</span>' + \
+ '</li>\n') % (n,n)
+ s += '</ol>\n</div>\n'
+ return s
else:
s = '<' + self.tag
if self.args:
@@ -177,7 +196,7 @@ class HtmlWikiMarkup (WikiMarkup):
"""
nested = 0
-
+ references = []
def __init__(self, *args, **kwargs):
super(HtmlWikiMarkup, self).__init__(*args, **kwargs)
self.token_class['LINK'] = HtmlLinkNode
diff --git a/WikiTrans/wiki2texi.py b/WikiTrans/wiki2texi.py
index 9a12d37..39c70c6 100644
--- a/WikiTrans/wiki2texi.py
+++ b/WikiTrans/wiki2texi.py
@@ -103,6 +103,12 @@ class TexiTagNode(WikiTagNode):
parser._print("@anchor{%s}\n" % self.args['id'],
nl=True, escape=False)
self.content.format()
+ elif self.tag == 'ref':
+ parser._print('@footnote{', escape=False);
+ self.content.format();
+ parser._print('}', escape=False)
+ elif self.tag == 'references':
+ pass
else:
parser._print('<' + self.tag)
if self.args:
@@ -341,6 +347,7 @@ class TexiWikiMarkup (WikiMarkup):
self.acc.trimpara()
return self._end_print()
+
diff --git a/WikiTrans/wiki2text.py b/WikiTrans/wiki2text.py
index 35f1aff..dc2e003 100644
--- a/WikiTrans/wiki2text.py
+++ b/WikiTrans/wiki2text.py
@@ -104,7 +104,7 @@ class TextLinkNode(WikiSeqNode):
ns = self.parser.wiki_ns_name(qual)
if ns:
if ns == 'NS_IMAGE':
- if not self.parser.references:
+ if not self.parser.showrefs:
return ""
text = "[%s: %s]" % (qual, text if text else arg)
tgt = self.image_base + '/' + \
@@ -121,7 +121,7 @@ class TextLinkNode(WikiSeqNode):
tgt = self.parser.mktgt(tgt)
else:
tgt = self.parser.mktgt(arg)
- if self.parser.references:
+ if self.parser.showrefs:
return "%s (see %s) " % (text, tgt)
elif not text or text == '':
return arg
@@ -190,6 +190,12 @@ class TextTagNode(WikiTagNode):
self.parser.nested += 1
s = self.content.format()
self.parser.nested -= 1
+ elif self.tag == 'ref':
+ s = '[%d]' % (self.idx+1)
+ elif self.tag == 'references':
+ s = '\nReferences:\n'
+ for ref in self.parser.references:
+ s += ('[%d]. ' % (ref.idx+1)) + ref.content.format() + '\n'
else:
s = '<' + self.tag
if self.args:
@@ -206,19 +212,22 @@ class TextWikiMarkup (WikiMarkup):
# Output width
width = 78
# Do not show references.
- references = False
+ showrefs = False
# Provide a minimum markup
markup = True
# Number of current element in the environment
num = 0
+
+ # Array of footnote references
+ references = []
def __init__(self, *args, **keywords):
super(TextWikiMarkup,self).__init__(*args, **keywords)
if 'width' in keywords:
self.width = keywords['width']
if 'refs' in keywords:
- self.references = keywords['refs']
+ self.showrefs = keywords['refs']
if 'markup' in keywords:
self.markup = keywords['markup']
self.token_class['SEQ'] = TextSeqNode
diff --git a/WikiTrans/wikidump.py b/WikiTrans/wikidump.py
index 5b257f9..7457dfa 100644
--- a/WikiTrans/wikidump.py
+++ b/WikiTrans/wikidump.py
@@ -19,8 +19,16 @@ from WikiTrans.wikitoken import *
import json
from WikiTrans.wikimarkup import WikiMarkup
+class DumpReferences(object):
+ idx = 0
+ def __len__(self):
+ return self.idx + 1
+ def append(self, obj):
+ self.idx += 1
+
class DumpWikiMarkup(WikiMarkup):
indent = None
+ references = DumpReferences()
def __init__(self, **kwarg):
n = kwarg.pop('indent', None)
if n != None:
diff --git a/WikiTrans/wikimarkup.py b/WikiTrans/wikimarkup.py
index af97063..0987da2 100644
--- a/WikiTrans/wikimarkup.py
+++ b/WikiTrans/wikimarkup.py
@@ -114,7 +114,7 @@ class BaseWikiMarkup(object):
newline = 0
tree = None
- tags = [ 'code', 'nowiki', 'tt', 'div' ]
+ tags = [ 'code', 'nowiki', 'tt', 'div', 'ref', 'references' ]
debug_level = 0
diff --git a/WikiTrans/wikitoken.py b/WikiTrans/wikitoken.py
index 5606ecc..2238a66 100644
--- a/WikiTrans/wikitoken.py
+++ b/WikiTrans/wikitoken.py
@@ -61,6 +61,8 @@ class WikiNode(object):
class WikiContentNode(WikiNode):
content = None
+ def format(self):
+ pass
@jsonencoder
def jsonEncode(self):
ret = {}
@@ -114,13 +116,20 @@ class WikiTagNode(WikiContentNode):
tag = None
isblock = False
args = None
+ idx = None
+ def __init__(self, *args, **keywords):
+ super(WikiTagNode, self).__init__(*args, **keywords)
+ if self.type == 'TAG' and self.tag == 'ref' and hasattr(self.parser,'references'):
+ self.idx = len(self.parser.references)
+ self.parser.references.append(self)
@jsonencoder
def jsonEncode(self):
return {
'tag': self.tag,
'isblock': self.isblock,
'args': self.args.tab if self.args else None,
- 'content': self.content.jsonEncode()
+ 'content': self.content.jsonEncode() if self.content else None,
+ 'idx': self.idx
}
class WikiRefNode(WikiContentNode):

Return to:

Send suggestions and report system problems to the System administrator.