##// END OF EJS Templates
Crop if no endtag is given
jakobgager -
Show More
@@ -1,107 +1,107 b''
1 """Citation handling for LaTeX output."""
1 """Citation handling for LaTeX output."""
2
2
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2013, the IPython Development Team.
4 # Copyright (c) 2013, the IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 from HTMLParser import HTMLParser
14 from HTMLParser import HTMLParser
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Functions
17 # Functions
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 __all__ = ['citation2latex']
20 __all__ = ['citation2latex']
21
21
22
22
23 def citation2latex(s):
23 def citation2latex(s):
24 """Parse citations in Markdown cells.
24 """Parse citations in Markdown cells.
25
25
26 This looks for HTML tags having a data attribute names `data-cite`
26 This looks for HTML tags having a data attribute names `data-cite`
27 and replaces it by the call to LaTeX cite command. The tranformation
27 and replaces it by the call to LaTeX cite command. The tranformation
28 looks like this:
28 looks like this:
29
29
30 `<cite data-cite="granger">(Granger, 2013)</cite>`
30 `<cite data-cite="granger">(Granger, 2013)</cite>`
31
31
32 Becomes
32 Becomes
33
33
34 `\\cite{granger}`
34 `\\cite{granger}`
35
35
36 Any HTML tag can be used, which allows the citations to be formatted
36 Any HTML tag can be used, which allows the citations to be formatted
37 in HTML in any manner.
37 in HTML in any manner.
38 """
38 """
39 parser = CitationParser()
39 parser = CitationParser()
40 parser.feed(s)
40 parser.feed(s)
41 parser.close()
41 parser.close()
42 outtext = u''
42 outtext = u''
43 startpos = 0
43 startpos = 0
44 for citation in parser.citelist:
44 for citation in parser.citelist:
45 outtext += s[startpos:citation[1]]
45 outtext += s[startpos:citation[1]]
46 outtext += '\\cite{%s}'%citation[0]
46 outtext += '\\cite{%s}'%citation[0]
47 startpos = citation[2]
47 startpos = citation[2] if len(citation)==3 else -1
48 outtext += s[startpos:]
48 outtext += s[startpos:] if startpos <> -1 else ''
49 return outtext
49 return outtext
50
50
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52 # Classes
52 # Classes
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54 class CitationParser(HTMLParser):
54 class CitationParser(HTMLParser):
55 """Citation Parser
55 """Citation Parser
56
56
57 Replaces html tags with data-cite attribute with respective latex \\cite.
57 Replaces html tags with data-cite attribute with respective latex \\cite.
58
58
59 Inherites from HTMLParser, overrides:
59 Inherites from HTMLParser, overrides:
60 - handle_starttag
60 - handle_starttag
61 - handle_endtag
61 - handle_endtag
62 """
62 """
63 # number of open tags
63 # number of open tags
64 opentags = None
64 opentags = None
65 # list of found citations
65 # list of found citations
66 citelist = None
66 citelist = None
67 # active citation tag
67 # active citation tag
68 citetag = None
68 citetag = None
69
69
70 def __init__(self):
70 def __init__(self):
71 self.citelist = []
71 self.citelist = []
72 self.opentags = 0
72 self.opentags = 0
73 HTMLParser.__init__(self)
73 HTMLParser.__init__(self)
74
74
75 def get_offset(self):
75 def get_offset(self):
76 # Compute startposition in source
76 # Compute startposition in source
77 lin, offset = self.getpos()
77 lin, offset = self.getpos()
78 pos = 0
78 pos = 0
79 for i in range(lin-1):
79 for i in range(lin-1):
80 pos = self.data.find('\n',pos) + 1
80 pos = self.data.find('\n',pos) + 1
81 return pos + offset
81 return pos + offset
82
82
83 def handle_starttag(self, tag, attrs):
83 def handle_starttag(self, tag, attrs):
84 # for each tag check if attributes are present and if no citation is active
84 # for each tag check if attributes are present and if no citation is active
85 if self.opentags == 0 and len(attrs)>0:
85 if self.opentags == 0 and len(attrs)>0:
86 for atr, data in attrs:
86 for atr, data in attrs:
87 if atr.lower() == 'data-cite':
87 if atr.lower() == 'data-cite':
88 self.citetag = tag
88 self.citetag = tag
89 self.opentags = 1
89 self.opentags = 1
90 self.citelist.append([data, self.get_offset()])
90 self.citelist.append([data, self.get_offset()])
91 return
91 return
92
92
93 if tag == self.citetag:
93 if tag == self.citetag:
94 # found an open citation tag but not the starting one
94 # found an open citation tag but not the starting one
95 self.opentags += 1
95 self.opentags += 1
96
96
97 def handle_endtag(self, tag):
97 def handle_endtag(self, tag):
98 if tag == self.citetag:
98 if tag == self.citetag:
99 # found citation tag check if starting one
99 # found citation tag check if starting one
100 if self.opentags == 1:
100 if self.opentags == 1:
101 pos = self.get_offset()
101 pos = self.get_offset()
102 self.citelist[-1].append(pos+len(tag)+3)
102 self.citelist[-1].append(pos+len(tag)+3)
103 self.opentags -= 1
103 self.opentags -= 1
104
104
105 def feed(self, data):
105 def feed(self, data):
106 self.data = data
106 self.data = data
107 HTMLParser.feed(self, data)
107 HTMLParser.feed(self, data)
General Comments 0
You need to be logged in to leave comments. Login now