git.iko.soy/typeable/pandoc
on master help sign in
Jira writer: improve div/panel handling

Include div attributes in panels, always render divs with class `panel` as panels, and avoid nesting of panels.

commita8aa301428752d96cdd58d7f4ecaa7d054f3505d authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 13 Mar 2021, 12:10 (6 years ago) parent894ed8e
2 files changed+58−11
src/Text/Pandoc/Writers/Jira.hs+28−11 test/Tests/Writers/Jira.hs+30
src/Text/Pandoc/Writers/Jira.hs+28−11
@@ -39,11 +39,17 @@ writeJira :: PandocMonad m => WriterOptions -> Pandoc -> m Text
3939writeJira opts = runDefaultConverter (writerWrapText opts) (pandocToJira opts)
4040
4141-- | State to keep track of footnotes.
42−newtype ConverterState = ConverterState { stNotes :: [Text] }
42+data ConverterState = ConverterState
43+ { stNotes :: [Text] -- ^ Footnotes to be appended to the end of the text
44+ , stInPanel :: Bool -- ^ whether we are in a @{panel}@ block
45+ }
4346
4447-- | Initial converter state.
4548startState :: ConverterState
46−startState = ConverterState { stNotes = [] }
49+startState = ConverterState
50+ { stNotes = []
51+ , stInPanel = False
52+ }
4753
4854-- | Converter monad
4955type JiraConverter m = ReaderT WrapOption (StateT ConverterState m)
@@ -126,14 +132,20 @@ toJiraCode :: PandocMonad m
126132 -> Text
127133 -> JiraConverter m [Jira.Block]
128134toJiraCode (ident, classes, _attribs) code = do
129− let addAnchor b = if T.null ident
130− then b
131− else [Jira.Para (singleton (Jira.Anchor ident))] <> b
132− return . addAnchor . singleton $
135+ return . addAnchor ident . singleton $
133136 case find (\c -> T.toLower c `elem` knownLanguages) classes of
134137 Nothing -> Jira.NoFormat mempty code
135138 Just l -> Jira.Code (Jira.Language l) mempty code
136139
140+-- | Prepends an anchor with the given identifier.
141+addAnchor :: Text -> [Jira.Block] -> [Jira.Block]
142+addAnchor ident =
143+ if T.null ident
144+ then id
145+ else \case
146+ Jira.Para xs : bs -> (Jira.Para (Jira.Anchor ident : xs) : bs)
147+ bs -> (Jira.Para (singleton (Jira.Anchor ident)) : bs)
148+
137149-- | Creates a Jira definition list
138150toJiraDefinitionList :: PandocMonad m
139151 => [([Inline], [[Block]])]
@@ -149,11 +161,16 @@ toJiraDefinitionList defItems = do
149161toJiraPanel :: PandocMonad m
150162 => Attr -> [Block]
151163 -> JiraConverter m [Jira.Block]
152−toJiraPanel attr blocks = do
153− jiraBlocks <- toJiraBlocks blocks
154− return $ if attr == nullAttr
155− then jiraBlocks
156− else singleton (Jira.Panel [] jiraBlocks)
164+toJiraPanel (ident, classes, attribs) blocks = do
165+ inPanel <- gets stInPanel
166+ if inPanel || ("panel" `notElem` classes && null attribs)
167+ then addAnchor ident <$> toJiraBlocks blocks
168+ else do
169+ modify $ \st -> st{ stInPanel = True }
170+ jiraBlocks <- toJiraBlocks blocks
171+ modify $ \st -> st{ stInPanel = inPanel }
172+ let params = map (uncurry Jira.Parameter) attribs
173+ return $ singleton (Jira.Panel params $ addAnchor ident jiraBlocks)
157174
158175-- | Creates a Jira header
159176toJiraHeader :: PandocMonad m
test/Tests/Writers/Jira.hs+30
@@ -79,4 +79,34 @@ tests =
7979 "{noformat}\npreformatted\n text.\n{noformat}"
8080 ]
8181 ]
82+
83+ , testGroup "blocks"
84+ [ testGroup "div"
85+ [ "empty attributes" =:
86+ divWith nullAttr (para "interesting text") =?>
87+ "interesting text"
88+
89+ , "just identifier" =:
90+ divWith ("a", [], []) (para "interesting text") =?>
91+ "{anchor:a}interesting text"
92+
93+ , "with class 'panel'" =:
94+ divWith ("", ["panel"], []) (para "Contents!") =?>
95+ "{panel}\nContents\\!\n{panel}\n"
96+
97+ , "panel with id" =:
98+ divWith ("b", ["panel"], []) (para "text") =?>
99+ "{panel}\n{anchor:b}text\n{panel}\n"
100+
101+ , "title attribute" =:
102+ divWith ("", [], [("title", "Gimme!")]) (para "Contents!") =?>
103+ "{panel:title=Gimme!}\nContents\\!\n{panel}\n"
104+
105+ , "nested panels" =:
106+ let panelAttr = ("", ["panel"], [])
107+ in divWith panelAttr (para "hi" <>
108+ divWith panelAttr (para "wassup?")) =?>
109+ "{panel}\nhi\n\nwassup?\n{panel}\n"
110+ ]
111+ ]
82112 ]