git.iko.soy/typeable/pandoc
on master help sign in
[Docx Reader] Refactor/update smushInlines
48cef91Nikolay Yakimov
src/Text/Pandoc/Readers/Docx/Combine.hs+32−44
@@ -4,7 +4,8 @@
4{- |
4{- |
5 Module : Text.Pandoc.Readers.Docx.Combine
5 Module : Text.Pandoc.Readers.Docx.Combine
6 Copyright : © 2014-2020 Jesse Rosenthal <jrosenthal@jhu.edu>,
6 Copyright : © 2014-2020 Jesse Rosenthal <jrosenthal@jhu.edu>,
7− 2014-2020 John MacFarlane <jgm@berkeley.edu>
7+ 2014-2020 John MacFarlane <jgm@berkeley.edu>,
8+ 2020 Nikolay Yakimov <root@livid.pp.ru>
8 License : GNU GPL, version 2 or above
9 License : GNU GPL, version 2 or above
9
10
10 Maintainer : Jesse Rosenthal <jrosenthal@jhu.edu>
11 Maintainer : Jesse Rosenthal <jrosenthal@jhu.edu>
@@ -59,7 +60,9 @@ module Text.Pandoc.Readers.Docx.Combine ( smushInlines
59 where
60 where
60
61
61import Data.List
62import Data.List
63+import Data.Bifunctor
62−import Data.Sequence (ViewL (..), ViewR (..), viewl, viewr, (><), (|>))
64+import Data.Sequence ( ViewL (..), ViewR (..), viewl, viewr, spanr, spanl
65+ , (><), (|>) )
63import qualified Data.Sequence as Seq (null)
66import qualified Data.Sequence as Seq (null)
64import Text.Pandoc.Builder
67import Text.Pandoc.Builder
65
68
@@ -80,14 +83,14 @@ spaceOutInlinesR ms = (stackInlines fs (l <> m'), r)
80spaceOutInlines :: Inlines -> (Inlines, Inlines, Inlines)
83spaceOutInlines :: Inlines -> (Inlines, Inlines, Inlines)
81spaceOutInlines ils =
84spaceOutInlines ils =
82 let (fs, ils') = unstackInlines ils
85 let (fs, ils') = unstackInlines ils
83− contents = unMany ils'
86+ (left, (right, contents')) = second (spanr isSpace) $ spanl isSpace $ unMany ils'
84− left = case viewl contents of
87+ -- NOTE: spanr counterintuitively returns suffix as the FIRST tuple element
85− (Space :< _) -> space
86− _ -> mempty
87− right = case viewr contents of
88− (_ :> Space) -> space
89− _ -> mempty in
90− (left, stackInlines fs $ trimInlines . Many $ contents, right)
88+ in (Many left, stackInlines fs $ Many contents', Many right)
89+
90+isSpace :: Inline -> Bool
91+isSpace Space = True
92+isSpace SoftBreak = True
93+isSpace _ = False
91
94
92stackInlines :: [Modifier Inlines] -> Inlines -> Inlines
95stackInlines :: [Modifier Inlines] -> Inlines -> Inlines
93stackInlines [] ms = ms
96stackInlines [] ms = ms
@@ -99,39 +102,24 @@ stackInlines (Modifier f : fs) ms =
99stackInlines (AttrModifier f attr : fs) ms = f attr $ stackInlines fs ms
102stackInlines (AttrModifier f attr : fs) ms = f attr $ stackInlines fs ms
100
103
101unstackInlines :: Inlines -> ([Modifier Inlines], Inlines)
104unstackInlines :: Inlines -> ([Modifier Inlines], Inlines)
102−unstackInlines ms = case ilModifier ms of
105+unstackInlines ms = case ilModifierAndInnards ms of
103− NullModifier -> ([], ms)
106+ (NullModifier, _) -> ([], ms)
104− _ -> (f : fs, ms') where
105− f = ilModifier ms
106− (fs, ms') = unstackInlines $ ilInnards ms
107+ (f, innards ) -> first (f :) $ unstackInlines innards
107−
108+
108−ilModifier :: Inlines -> Modifier Inlines
109+ilModifierAndInnards :: Inlines -> (Modifier Inlines, Inlines)
109−ilModifier ils = case viewl (unMany ils) of
110+ilModifierAndInnards ils = case viewl $ unMany ils of
110− (x :< xs) | Seq.null xs -> case x of
111+ x :< xs | Seq.null xs -> case x of
111− (Emph _) -> Modifier emph
112+ Emph lst -> (Modifier emph, fromList lst)
112− (Strong _) -> Modifier strong
113+ Strong lst -> (Modifier strong, fromList lst)
113− (SmallCaps _) -> Modifier smallcaps
114+ SmallCaps lst -> (Modifier smallcaps, fromList lst)
114− (Strikeout _) -> Modifier strikeout
115+ Strikeout lst -> (Modifier strikeout, fromList lst)
116+ Underline lst -> (Modifier underline, fromList lst)
115− (Superscript _) -> Modifier superscript
117+ Superscript lst -> (Modifier superscript, fromList lst)
116− (Subscript _) -> Modifier subscript
118+ Subscript lst -> (Modifier subscript, fromList lst)
117− (Link attr _ tgt) -> Modifier $ linkWith attr (fst tgt) (snd tgt)
119+ Link attr lst tgt -> (Modifier $ linkWith attr (fst tgt) (snd tgt), fromList lst)
118− (Span attr _) -> AttrModifier spanWith attr
120+ Span attr lst -> (AttrModifier spanWith attr, fromList lst)
119− _ -> NullModifier
121+ _ -> (NullModifier, ils)
120− _ -> NullModifier
122+ _ -> (NullModifier, ils)
121−
122−ilInnards :: Inlines -> Inlines
123−ilInnards ils = case viewl (unMany ils) of
124− (x :< xs) | Seq.null xs -> case x of
125− (Emph lst) -> fromList lst
126− (Strong lst) -> fromList lst
127− (SmallCaps lst) -> fromList lst
128− (Strikeout lst) -> fromList lst
129− (Superscript lst) -> fromList lst
130− (Subscript lst) -> fromList lst
131− (Link _ lst _) -> fromList lst
132− (Span _ lst) -> fromList lst
133− _ -> ils
134− _ -> ils
135
123
136inlinesL :: Inlines -> (Inlines, Inlines)
124inlinesL :: Inlines -> (Inlines, Inlines)
137inlinesL ils = case viewl $ unMany ils of
125inlinesL ils = case viewl $ unMany ils of
@@ -162,7 +150,7 @@ combineSingletonInlines x y =
162 in
150 in
163 case null shared of
151 case null shared of
164 True | isEmpty xs && isEmpty ys ->
152 True | isEmpty xs && isEmpty ys ->
165− stackInlines (x_rem_attr ++ y_rem_attr) mempty
153+ stackInlines (x_rem_attr <> y_rem_attr) mempty
166 | isEmpty xs ->
154 | isEmpty xs ->
167 let (sp, y') = spaceOutInlinesL y in
155 let (sp, y') = spaceOutInlinesL y in
168 stackInlines x_rem_attr mempty <> sp <> y'
156 stackInlines x_rem_attr mempty <> sp <> y'
test/docx/golden/inline_formatting.docx

Binary file not shown.

test/docx/inline_formatting.native+1−1
@@ -1,6 +1,6 @@
1Pandoc (Meta {unMeta = fromList []})
1Pandoc (Meta {unMeta = fromList []})
2[Para [Str "Regular",Space,Str "text",Space,Emph [Str "italics"],Space,Strong [Str "bold",Space,Emph [Str "bold",Space,Str "italics"]],Str "."]
2[Para [Str "Regular",Space,Str "text",Space,Emph [Str "italics"],Space,Strong [Str "bold",Space,Emph [Str "bold",Space,Str "italics"]],Str "."]
3,Para [Str "This",Space,Str "is",Space,SmallCaps [Str "Small",Space,Str "Caps"],Str ",",Space,Str "and",Space,Str "this",Space,Str "is",Space,Strikeout [Str "strikethrough"],Str "."]
3,Para [Str "This",Space,Str "is",Space,SmallCaps [Str "Small",Space,Str "Caps"],Str ",",Space,Str "and",Space,Str "this",Space,Str "is",Space,Strikeout [Str "strikethrough"],Str "."]
4−,Para [Str "Some",Space,Str "people",Space,Str "use",Space,Underline [Str "single",Space,Str "underlines",Space,Str "for",Space],Emph [Underline [Str "emphasis"]],Str "."]
4+,Para [Str "Some",Space,Str "people",Space,Str "use",Space,Underline [Str "single",Space,Str "underlines",Space,Str "for",Space,Emph [Str "emphasis"]],Str "."]
5,Para [Str "Above",Space,Str "the",Space,Str "line",Space,Str "is",Space,Superscript [Str "superscript"],Space,Str "and",Space,Str "below",Space,Str "the",Space,Str "line",Space,Str "is",Space,Subscript [Str "subscript"],Str "."]
5,Para [Str "Above",Space,Str "the",Space,Str "line",Space,Str "is",Space,Superscript [Str "superscript"],Space,Str "and",Space,Str "below",Space,Str "the",Space,Str "line",Space,Str "is",Space,Subscript [Str "subscript"],Str "."]
6,Para [Str "A",Space,Str "line",LineBreak,Str "break."]]
6,Para [Str "A",Space,Str "line",LineBreak,Str "break."]]