MediaWiki reader/writer: allow definition on same line as term.

Closes #10708.
This commit is contained in:
John MacFarlane
2025-03-21 21:54:47 -07:00
parent 6b467b55a0
commit 59fb9c40de
3 changed files with 49 additions and 13 deletions
+13 -11
View File
@@ -55,6 +55,7 @@ readMediaWiki opts s = do
, mwIdentifierList = Set.empty
, mwLogMessages = []
, mwInTT = False
, mwAllowNewlines = True
}
sources
case parsed of
@@ -68,6 +69,7 @@ data MWState = MWState { mwOptions :: ReaderOptions
, mwIdentifierList :: Set.Set Text
, mwLogMessages :: [LogMessage]
, mwInTT :: Bool
, mwAllowNewlines :: Bool
}
type MWParser m = ParsecT Sources MWState m
@@ -453,9 +455,8 @@ defListTerm = do
guardColumnOne
char ';'
skipMany spaceChar
pos' <- getPosition
anyLine >>= parseFromString (do setPosition pos'
trimInlines . mconcat <$> many inline)
trimInlines . mconcat <$> many (notFollowedBy (oneOf ":\r\n") *> inline) <*
optional newline
listStart :: PandocMonad m => Char -> MWParser m ()
listStart c = char c *> notFollowedBy listStartChar
@@ -472,7 +473,7 @@ li = lookAhead (htmlTag (~== TagOpen ("li" :: Text) [])) *>
listItem :: PandocMonad m => Char -> MWParser m Blocks
listItem c = try $ do
guardColumnOne
guardColumnOne <|> guard (c == ':') -- def can start on same line as term
extras <- many (try $ char c <* lookAhead listStartChar)
if null extras
then listItem' c
@@ -607,13 +608,14 @@ whitespace = B.space <$ (skipMany1 spaceChar <|> htmlComment)
<|> B.softbreak <$ endline
endline :: PandocMonad m => MWParser m ()
endline = () <$ try (newline <*
notFollowedBy spaceChar <*
notFollowedBy newline <*
notFollowedBy' hrule <*
notFollowedBy tableStart <*
notFollowedBy' header <*
notFollowedBy anyListStart)
endline = do
getState >>= guard . mwAllowNewlines
() <$ try (newline <* notFollowedBy spaceChar <*
notFollowedBy newline <*
notFollowedBy' hrule <*
notFollowedBy tableStart <*
notFollowedBy' header <*
notFollowedBy anyListStart)
imageIdentifier :: PandocMonad m => MWParser m ()
imageIdentifier = try $ do
+12 -2
View File
@@ -38,6 +38,7 @@ import Text.Pandoc.XML (escapeStringForXML)
data WriterState = WriterState {
stNotes :: Bool -- True if there are notes
, stOptions :: WriterOptions -- writer options
, stInDefLabel :: Bool -- True if in definition list label
}
data WriterReader = WriterReader {
@@ -51,7 +52,8 @@ type MediaWikiWriter m = ReaderT WriterReader (StateT WriterState m)
-- | Convert Pandoc to MediaWiki.
writeMediaWiki :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeMediaWiki opts document =
let initialState = WriterState { stNotes = False, stOptions = opts }
let initialState = WriterState {
stNotes = False, stOptions = opts, stInDefLabel = False }
env = WriterReader { options = opts, listLevel = [], useTags = False }
in evalStateT (runReaderT (pandocToMediaWiki document) env) initialState
@@ -237,7 +239,9 @@ definitionListItemToMediaWiki :: PandocMonad m
=> ([Inline],[[Block]])
-> MediaWikiWriter m Text
definitionListItemToMediaWiki (label, items) = do
modify $ \st -> st{ stInDefLabel = True }
labelText <- inlineListToMediaWiki label
modify $ \st -> st{ stInDefLabel = False }
contents <- mapM blockListToMediaWiki items
tags <- asks useTags
if tags
@@ -446,7 +450,13 @@ inlineToMediaWiki (Cite _ lst) = inlineListToMediaWiki lst
inlineToMediaWiki (Code _ str) =
return $ "<code>" <> escapeText str <> "</code>"
inlineToMediaWiki (Str str) = return $ escapeText str
inlineToMediaWiki (Str str) = do
inDefLabel <- gets stInDefLabel
return $
if inDefLabel
then T.intercalate "<nowiki>:</nowiki>" $
map escapeText $ T.splitOn ":" str
else escapeText str
inlineToMediaWiki (Math mt str) = return $
"<math display=\"" <>
+24
View File
@@ -0,0 +1,24 @@
```
% pandoc -f html -t mediawiki
<dl>
<dt>Case 1: Both subsets are non-empty</dt>
<dd>
In this case, …
</dd>
</dl>
^D
; Case 1<nowiki>:</nowiki> Both subsets are non-empty
: In this case, …
```
```
% pandoc -f mediawiki -t html
; term : definition
^D
<dl>
<dt>term</dt>
<dd>
definition
</dd>
</dl>
```