-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefparser.hs
More file actions
178 lines (143 loc) · 7.62 KB
/
refparser.hs
File metadata and controls
178 lines (143 loc) · 7.62 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module Main where
import System.IO()
import System.Environment
import qualified Data.Map.Strict as Map
import Text.Regex
import Text.Regex.Posix
import Data.String.Utils
-----------------------------------------------------------------------------------------------------------------------------
matchNext :: String -> String -> Maybe (String, String, String, [String])
matchNext = matchRegexAll . makeRegex
-----------------------------------------------------------------------------------------------------------------------------
-- Maps objects to numbers
type ObjectMap = Map.Map String Int
-- Maps counter names to counter values
type CounterMap = Map.Map String Int
objectLabelRegex :: String
objectLabelRegex = "\\\\label(\\[([^]]*)\\])?{([^}]*)}"
objectRefRegex :: String
objectRefRegex = "\\\\ref{([^}]*)}"
defaultCounterName :: String
defaultCounterName = ""
matchObjectsHelper :: CounterMap -> String -> ObjectMap -> (String, ObjectMap)
matchObjectsHelper counters left objmap = case matchNext objectLabelRegex left of
Just (textbefore, _, textafter, [_, counter, label]) -> (textbefore ++ fst next, snd next)
where
newcounters = Map.insertWith (+) counter 1 counters
next = matchObjectsHelper newcounters textafter $ Map.insert label (newcounters Map.! counter) objmap
Nothing -> (left, objmap)
_ -> (left, objmap)
matchObjects :: String -> (String, ObjectMap)
matchObjects input = matchObjectsHelper Map.empty input Map.empty
formatObjectRefLookup :: [String] -> ObjectMap -> String
formatObjectRefLookup [label] objmap = case Map.lookup label objmap of
Just counter -> show counter
Nothing -> "\\ref{" ++ label ++ "}"
formatObjectRefLookup _ _ = []
addObjectRefs :: String -> ObjectMap -> String
addObjectRefs left objmap = case matchNext objectRefRegex left of
Just (textbefore, _, textafter, submatches) -> textbefore ++ formatObjectRefLookup submatches objmap ++ addObjectRefs textafter objmap
Nothing -> left
parseObjects :: String -> String
parseObjects = uncurry addObjectRefs . matchObjects
-----------------------------------------------------------------------------------------------------------------------------
-- Maps labels to numbers
type FigureMap = Map.Map String Int
figureLabelRegex :: String
figureLabelRegex = "!\\[([^]\\]*)(\\\\label{([^}]*)}([^]\\]*))?\\]\\((.*)\\)"
figureRefRegex :: String
figureRefRegex = "\\\\ref{([^}]*)}"
formatCaption :: Int -> String -> String -> String
formatCaption counter [] [] = "Figure " ++ show counter
formatCaption counter c1 [] = "Figure " ++ show counter ++ ": " ++ strip c1
formatCaption counter [] c2 = "Figure " ++ show counter ++ ": " ++ strip c2
formatCaption counter c1 c2 = "Figure " ++ show counter ++ ": " ++ strip c1 ++ " " ++ strip c2
figureCaptionFromSubMatches :: Int -> [String] -> (String, String)
figureCaptionFromSubMatches counter [caption1, _, label, caption2, url] = (label, "")
figureCaptionFromSubMatches _ _ = ([],[])
matchFiguresHelper :: Int -> String -> FigureMap -> (String, FigureMap)
matchFiguresHelper counter left figmap = case matchNext figureLabelRegex left of
Just (textbefore, _, textafter, submatches) -> (textbefore ++ snd caption ++ fst next, snd next)
where
caption = figureCaptionFromSubMatches counter submatches
next = matchFiguresHelper (counter + 1) textafter $ Map.insert (fst caption) counter figmap
Nothing -> (left, figmap)
matchFigures :: String -> (String, FigureMap)
matchFigures input = matchFiguresHelper 1 input Map.empty
formatFigureRefLookup :: [String] -> FigureMap -> String
formatFigureRefLookup [label] figmap = case Map.lookup label figmap of
Just counter -> show counter
Nothing -> "\\ref{" ++ label ++ "}"
formatFigureRefLookup _ _ = []
addFigureRefs :: String -> FigureMap -> String
addFigureRefs left figmap = case matchNext figureRefRegex left of
Just (textbefore, _, textafter, submatches) -> textbefore ++ formatFigureRefLookup submatches figmap ++ addFigureRefs textafter figmap
Nothing -> left
parseFigures :: String -> String
parseFigures = uncurry addFigureRefs . matchFigures
-----------------------------------------------------------------------------------------------------------------------------
type SectionMap = Map.Map String [Int]
-- Maximum section depth
maxLevels :: Int
maxLevels = 4
sectionHeaderRegex :: String
sectionHeaderRegex = "^([ ]*)(#+)[ ]*(.+)$*"
sectionLabelRegex :: String
sectionLabelRegex = "\r?\n?^[^\\]*\\\\label{([^}]*)}.*$\r?\n?"
sectionRefRegex :: String
sectionRefRegex = "\\\\ref{([^}]*)}"
-- Update counters from ### textmatch
updateCounters :: [Int] -> String -> [Int]
updateCounters counters hashlevels
| length counters < length hashlevels = counters
| otherwise = take (length hashlevels - 1) counters ++ [counters !! (length hashlevels - 1) + 1] ++ replicate (length counters - length hashlevels) 0
-- Finds labels in a section text and saves them in a map and removes them from the string
matchSectionLabels :: [Int] -> String -> SectionMap -> (String, SectionMap)
matchSectionLabels counters left secmap = case matchNext sectionLabelRegex left of
Just (textbefore, _, textafter, [label]) -> (textbefore ++ fst next, snd next)
where next = matchSectionLabels counters textafter $ Map.insert label counters secmap
Nothing -> (left, secmap)
_ -> (left, secmap)
addDots :: [Int] -> String
addDots [] = []
addDots (0:_) = []
addDots [x] = show x
addDots (x:0:_) = show x
addDots (x:xs) = show x ++ "." ++ addDots xs
formatSectionHeader :: String -> [Int] -> String -> String
formatSectionHeader hashlevels counters caption
| length hashlevels <= maxLevels = hashlevels ++ " " ++ sectionNumber ++ " " ++ caption ++ " {#section-" ++ sectionNumber ++ "}"
| otherwise = hashlevels ++ " " ++ caption ++ " {#section-" ++ sectionNumber ++ "}"
where sectionNumber = addDots counters
matchSectionHeadersHelper :: [Int] -> String -> SectionMap -> (String, SectionMap)
matchSectionHeadersHelper counters left secmap = case matchNext sectionHeaderRegex left of
Just (textbefore, _, textafter, [prespace, hashlevels, caption]) -> (fst prevmatch ++ prespace ++ formatSectionHeader hashlevels newcounters caption ++ fst next, snd next)
where
newcounters = updateCounters counters hashlevels
prevmatch = matchSectionLabels counters textbefore secmap
next = matchSectionHeadersHelper newcounters textafter $ snd prevmatch
Nothing -> matchSectionLabels counters left secmap
_ -> matchSectionLabels counters left secmap
matchSections :: String -> (String, SectionMap)
matchSections input = matchSectionHeadersHelper (replicate 10 0) input Map.empty
formatSectionRefLookup :: [String] -> SectionMap -> String
formatSectionRefLookup [label] secmap = case Map.lookup label secmap of
Just counters -> "[" ++ sectionNumber ++ "](#section-" ++ sectionNumber ++ ")" where sectionNumber = addDots counters
Nothing -> "\\ref{" ++ label ++ "}"
formatSectionRefLookup _ _ = []
addSectionRefs :: String -> SectionMap -> String
addSectionRefs left secmap = case matchNext sectionRefRegex left of
Just (textbefore, _, textafter, submatches) -> textbefore ++ formatSectionRefLookup submatches secmap ++ addSectionRefs textafter secmap
Nothing -> left
parseSections :: String -> String
parseSections = uncurry addSectionRefs . matchSections
-----------------------------------------------------------------------------------------------------------------------------
-- Main function
main :: IO ()
main = do
args <- getArgs
case args of
[infile, outfile] -> do
text <- readFile infile
writeFile outfile $ parseObjects $ parseSections $ parseFigures text
_ -> putStrLn "Wrong number of arguments!\n\nUsage: refparser infile outfile"