@@ -0,0 +1,89 @@
1+{-
2+Copyright (C) 2014-2018 Jesse Rosenthal <jrosenthal@jhu.edu>
3+
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
8+
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
13+
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+-}
18+
19+{- |
20+ Module : Text.Pandoc.Readers.Docx.Fields
21+ Copyright : Copyright (C) 2014-2018 Jesse Rosenthal
22+ License : GNU GPL, version 2 or above
23+
24+ Maintainer : Jesse Rosenthal <jrosenthal@jhu.edu>
25+ Stability : alpha
26+ Portability : portable
27+
28+For parsing Field definitions in instText tags, as described in
29+ECMA-376-1:2016, §17.16.5 -}
30+
31+module Text.Pandoc.Readers.Docx.Fields ( FieldInfo(..)
32+ , parseFieldInfo
33+ ) where
34+
35+import Text.Parsec
36+import Text.Parsec.String (Parser)
37+
38+type URL = String
39+
40+data FieldInfo = HyperlinkField URL
41+ | UnknownField
42+ deriving (Show)
43+
44+parseFieldInfo :: String -> Either ParseError FieldInfo
45+parseFieldInfo = parse fieldInfo ""
46+
47+fieldInfo :: Parser FieldInfo
48+fieldInfo =
49+ (try $ HyperlinkField <$> hyperlink)
50+ <|>
51+ return UnknownField
52+
53+escapedQuote :: Parser String
54+escapedQuote = string "\\\""
55+
56+inQuotes :: Parser String
57+inQuotes = do
58+ (try escapedQuote) <|> (anyChar >>= (\c -> return [c]))
59+
60+quotedString :: Parser String
61+quotedString = do
62+ char '"'
63+ concat <$> manyTill inQuotes (try (char '"'))
64+
65+unquotedString :: Parser String
66+unquotedString = manyTill anyChar (try (space))
67+
68+fieldArgument :: Parser String
69+fieldArgument = quotedString <|> unquotedString
70+
71+-- there are other switches, but this is the only one I've seen in the wild so far, so it's the first one I'll implement. See §17.16.5.25
72+hyperlinkSwitch :: Parser (String, String)
73+hyperlinkSwitch = do
74+ sw <- string "\\l"
75+ spaces
76+ farg <- fieldArgument
77+ return (sw, farg)
78+
79+hyperlink :: Parser URL
80+hyperlink = do
81+ many space
82+ string "HYPERLINK"
83+ spaces
84+ farg <- fieldArgument
85+ switches <- (spaces *> many hyperlinkSwitch)
86+ let url = case switches of
87+ ("\\l", s) : _ -> farg ++ ('#': s)
88+ _ -> farg
89+ return url