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