git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1354 from jkr/literalTab

Parse literal tabs in docx

commit0d8e0e567479e723c986b2384c39a997f9065fa2 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 19 Jun 2014, 22:47 (12 years ago) parent5cb53a4da0d1d2
6 files changed+29−4
src/Text/Pandoc/Readers/Docx.hs+16−1 src/Text/Pandoc/Readers/Docx/Parse.hs+4−1 tests/Tests/Readers/Docx.hs+6−1 tests/docx.notes.native+1−1 tests/docx.tabs.docx tests/docx.tabs.native+2
src/Text/Pandoc/Readers/Docx.hs+16−1
@@ -134,10 +134,12 @@ codeDivs = ["SourceCode"]
134134runElemToInlines :: RunElem -> [Inline]
135135runElemToInlines (TextRun s) = strToInlines s
136136runElemToInlines (LnBrk) = [LineBreak]
137+runElemToInlines (Tab) = [Space]
137138
138139runElemToString :: RunElem -> String
139140runElemToString (TextRun s) = s
140141runElemToString (LnBrk) = ['\n']
142+runElemToString (Tab) = ['\t']
141143
142144runElemsToString :: [RunElem] -> String
143145runElemsToString = concatMap runElemToString
@@ -148,6 +150,19 @@ strNormalize (Str "" : ils) = strNormalize ils
148150strNormalize ((Str s) : (Str s') : l) = strNormalize ((Str (s++s')) : l)
149151strNormalize (il:ils) = il : (strNormalize ils)
150152
153+blockNormalize :: Block -> Block
154+blockNormalize (Plain (Space : ils)) = blockNormalize (Plain ils)
155+blockNormalize (Plain ils) = Plain $ strNormalize ils
156+blockNormalize (Para (Space : ils)) = blockNormalize (Para ils)
157+blockNormalize (Para ils) = Para $ strNormalize ils
158+blockNormalize (Header n attr (Space : ils)) =
159+ blockNormalize $ Header n attr ils
160+blockNormalize (Table (Space : ils) align width hdr cells) =
161+ blockNormalize $ Table ils align width hdr cells
162+blockNormalize (Table ils align width hdr cells) =
163+ Table (strNormalize ils) align width hdr cells
164+blockNormalize blk = blk
165+
151166runToInlines :: ReaderOptions -> Docx -> Run -> [Inline]
152167runToInlines _ _ (Run rs runElems)
153168 | isJust (rStyle rs) && (fromJust (rStyle rs)) `elem` codeSpans =
@@ -294,7 +309,7 @@ makeImagesSelfContained _ inline = inline
294309bodyToBlocks :: ReaderOptions -> Docx -> Body -> [Block]
295310bodyToBlocks opts docx (Body bps) =
296311 bottomUp removeEmptyPars $
297− bottomUp strNormalize $
312+ bottomUp blockNormalize $
298313 bottomUp spanRemove $
299314 bottomUp divRemove $
300315 map (makeHeaderAnchors) $
src/Text/Pandoc/Readers/Docx/Parse.hs+4−1
@@ -466,7 +466,7 @@ data Run = Run RunStyle [RunElem]
466466 | Endnote String
467467 deriving Show
468468
469−data RunElem = TextRun String | LnBrk
469+data RunElem = TextRun String | LnBrk | Tab
470470 deriving Show
471471
472472data RunStyle = RunStyle { isBold :: Bool
@@ -545,6 +545,9 @@ elemToRunElem ns element
545545 | qName (elName element) == "br" &&
546546 qURI (elName element) == (lookup "w" ns) =
547547 Just $ LnBrk
548+ | qName (elName element) == "tab" &&
549+ qURI (elName element) == (lookup "w" ns) =
550+ Just $ Tab
548551 | otherwise = Nothing
549552
550553
tests/Tests/Readers/Docx.hs+6−1
@@ -44,7 +44,12 @@ tests = [ testGroup "inlines"
4444 , testCompare
4545 "handling unicode input"
4646 "docx.unicode.docx"
47− "docx.unicode.native"]
47+ "docx.unicode.native"
48+ , testCompare
49+ "literal tabs"
50+ "docx.tabs.docx"
51+ "docx.tabs.native"
52+ ]
4853 , testGroup "blocks"
4954 [ testCompare
5055 "headers"
tests/docx.notes.native+1−1
@@ -1,2 +1,2 @@
11[Header 2 ("",[],[]) [Str "A",Space,Str "footnote"]
2−,Para [Str "Test",Space,Str "footnote.",Note [Para [Space,Str "My",Space,Str "note."]],Space,Str "Test",Space,Str "endnote.",Note [Para [Space,Str "This",Space,Str "is",Space,Str "an",Space,Str "endnote",Space,Str "at",Space,Str "the",Space,Str "end",Space,Str "of",Space,Str "the",Space,Str "document."]]]]
2+,Para [Str "Test",Space,Str "footnote.",Note [Para [Str "My",Space,Str "note."]],Space,Str "Test",Space,Str "endnote.",Note [Para [Str "This",Space,Str "is",Space,Str "an",Space,Str "endnote",Space,Str "at",Space,Str "the",Space,Str "end",Space,Str "of",Space,Str "the",Space,Str "document."]]]]
tests/docx.tabs.docxnew file

Binary file not shown.

tests/docx.tabs.nativenew file+2
@@ -0,0 +1,2 @@
1+[Para [Str "Some",Space,Str "text",Space,Str "separated",Space,Str "by",Space,Str "a",Space,Str "tab."]
2+,Para [Str "Tab-indented",Space,Str "text."]]