Skip to content

Commit

Permalink
RST reader: implement option lists.
Browse files Browse the repository at this point in the history
Closes #10318.
  • Loading branch information
jgm committed Oct 23, 2024
1 parent 7b08411 commit d61b50d
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Text/Pandoc/Readers/RST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ block :: PandocMonad m => RSTParser m Blocks
block = choice [ codeBlock
, blockQuote
, fieldList
, optionList
, referenceKey
, noteBlock
, citationBlock
Expand Down Expand Up @@ -314,6 +315,51 @@ fieldList = try $ do
[] -> return mempty
items' -> return $ B.definitionList items'

optionList :: PandocMonad m => RSTParser m Blocks
optionList = B.definitionList <$> many1 optionListItem

optionListItem :: PandocMonad m => RSTParser m (Inlines, [Blocks])
optionListItem = try $ do
opts <- snd <$> withRaw (do
let anyOpt = shortOpt <|> longOpt <|> dosOpt
anyOpt
many $ try (char ',' <* many spaceChar *> anyOpt))
-- at least two spaces
rawfirst <- try (char ' ' *> many1 (char ' ') *> anyLineNewline)
<|> try (mempty <$ skipMany spaceChar <* newline)
bodyElements <- do
raw <- option "" indentedBlock
parseFromString' parseBlocks $ (rawfirst <> raw) <> "\n\n"
optional blanklines
pure (B.code opts, [bodyElements])

shortOpt :: PandocMonad m => RSTParser m ()
shortOpt = try $ do
char '-'
alphaNum
optional $ try (optional (char ' ') *> optArg)

optArg :: PandocMonad m => RSTParser m ()
optArg = do
c <- letter <|> char '<'
if c == '<'
then () <$ manyTill (noneOf "<>") (char '>')
else skipMany (alphaNum <|> char '_' <|> char '-')

longOpt :: PandocMonad m => RSTParser m ()
longOpt = try $ do
char '-'
char '-'
alphaNum
skipMany1 (alphaNum <|> char '-' <|> char '_')
optional $ try (oneOf " =" *> optArg)

dosOpt :: PandocMonad m => RSTParser m ()
dosOpt = try $ do
char '/'
alphaNum <|> char '?'
optional $ try (char ' ' *> optArg)

--
-- line block
--
Expand Down
84 changes: 84 additions & 0 deletions test/command/10318.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
```
% pandoc -f rst -t html5
-a Output all.
-c arg Output just arg.
--long Output all day long.
/V A VMS/DOS-style option.
-p This option has two paragraphs in the description.
This is the first.
This is the second.
Blank lines may be omitted between options
(as above) or left in (as here and below).
--very-long-option A VMS-style option. Note the adjustment
for the required two spaces.
--an-even-longer-option
The description can also start on the next line.
-2, --two This option has two variants.
-f FILE, --file=FILE These two options are synonyms; both have
arguments.
-f <[path]file> Option argument placeholders must start with
a letter or be wrapped in angle brackets.
-d <src dest> Angle brackets are also required if an option
expects more than one argument.
^D
<dl>
<dt><code>-a</code></dt>
<dd>
<p>Output all.</p>
</dd>
<dt><code>-c arg</code></dt>
<dd>
<p>Output just arg.</p>
</dd>
<dt><code>--long</code></dt>
<dd>
<p>Output all day long.</p>
</dd>
<dt><code>/V</code></dt>
<dd>
<p>A VMS/DOS-style option.</p>
</dd>
<dt><code>-p</code></dt>
<dd>
<p>This option has two paragraphs in the description. This is the
first.</p>
<p>This is the second. Blank lines may be omitted between options (as
above) or left in (as here and below).</p>
</dd>
<dt><code>--very-long-option</code></dt>
<dd>
<p>A VMS-style option. Note the adjustment for the required two
spaces.</p>
</dd>
<dt><code>--an-even-longer-option</code></dt>
<dd>
<p>The description can also start on the next line.</p>
</dd>
<dt><code>-2, --two</code></dt>
<dd>
<p>This option has two variants.</p>
</dd>
<dt><code>-f FILE, --file=FILE</code></dt>
<dd>
<p>These two options are synonyms; both have arguments.</p>
</dd>
<dt><code>-f &lt;[path]file&gt;</code></dt>
<dd>
<p>Option argument placeholders must start with a letter or be wrapped
in angle brackets.</p>
</dd>
<dt><code>-d &lt;src dest&gt;</code></dt>
<dd>
<p>Angle brackets are also required if an option expects more than one
argument.</p>
</dd>
</dl>
```

0 comments on commit d61b50d

Please sign in to comment.