-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_add_eqn_references.py
57 lines (49 loc) · 1.96 KB
/
parse_add_eqn_references.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# coding: ascii
# Copyright (c) Mike Gibson, Craig Carter.
# Distributed under the terms of the MIT License.
"""
This provides methods for regular expression analysis of tex files,
with the purpose of parsing our Gibbs files.
"""
__author__ = "Mike Gibson"
__credits__ = "Craig Carter"
__copyright__ = "Copyright 2015, Mike Gibson"
__version__ = "1.0"
__maintainer__ = "Mike Gibson"
__email__ = "gibson.michael.a@gmail.com"
__date__ = "Nov 17, 2015"
import re
from more_itertools import unique_everseen
idealform = re.compile(r'\(\d{1,3}\)')
def ref_insert_line(line, form):
"""
Inserts \ref{} functions into tex files by substituting for a regex.
:param line: a line from a tex file, in the form of a string,
which we want to process and insert references into
:param form: a regular expression specification
for a string to be replaced.
:return: a string, wherein all of the strings specified by
form are replaced by \ref{form}
"""
# lineIterator = form.search(line)
# searchAndSub = []
# lineNew = line
# while searchAndSub is not None:
# searchAndSub = form.search(line)
# lineNew = lineNew.replace()
searchresults = form.findall(line)
iterableStrings = list(unique_everseen(searchresults))
lineNew = line
for substring in iterableStrings:
lineNew = lineNew.replace(substring, r'(\ref{' + substring[1:-1] + r'})')
return lineNew
tests = ["some text. An equation (67) appears! some more text",
"some text. something text in parens (dgwbea) appears! some more text",
"some text. thing that looks like an equation (6a) appears! some more text",
"bit of text. one Eq (67) two Eqs (92) are parsed.",
"bit of text. () should remain the same.",
"what about something that \d containes \( weird escapes? (55) here"]
# \d means any number, same as the set: [0-9]
# form = \(\d{1,3}\)
for test in tests:
print ref_insert_line(test, idealform)