New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap HTML in `raw` directive when working with ReStructuredText #3373

Closed
ice1000 opened this Issue Nov 7, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@ice1000
Member

ice1000 commented Nov 7, 2018

Ref: https://stackoverflow.com/a/8622176/7083401
Ref: http://docutils.sourceforge.net/docs/ref/rst/directives.html#raw-data-pass-through

We should wrap the rendered HTML code blocks into a raw directive.
Related codes:

-- | Preprocessor for reStructuredText.
literateRsT :: Position -> String -> [Layer]
literateRsT pos s = mkLayers pos$ rst s
where
rst :: String -> [(LayerRole, String)]
rst [] = []
rst s = maybe_code s
maybe_code s =
if r_comment `match` line then
not_code
else case r_code `match` line of
[] -> not_code
[[_, before, "::", after]] ->
-- Code starts
if null before || isBlank (last before) then
(Markup, line) : code rest
else
(Comment, before ++ ":") : (Markup, ":" ++ after) : code rest
_ -> __IMPOSSIBLE__
where
(line, rest) = getLine s
not_code = (Comment, line) : rst rest
-- Finds the next indented block in the input.
code :: String -> [(LayerRole, String)]
code [] = []
code s =
let (line, rest) = getLine s in
if all isSpace line then
(Markup, line) : code rest
else
let xs = takeWhile isBlank line in
if null xs
then maybe_code s
else (Code, line) : indented xs rest
-- Process an indented block.
indented :: String -> String -> [(LayerRole, String)]
indented _ [] = []
indented ind s =
let (line, rest) = getLine s in
if all isSpace line then
(Code, line) : indented ind rest
else if ind `isPrefixOf` line then
(Code, line) : indented ind rest
else
maybe_code s
-- Beginning of a code block.
r_code = rex "(.*)(::)([[:space:]]*)"
-- Beginning of a comment block.
r_comment = rex "[[:space:]]*\\.\\.([[:space:]].*)?"

code :: Bool -- ^ Whether to generate non-code contents as-is
-> [(Int, String, Aspects)]
-> Html
code asIs = mconcat . if asIs
then map mkMd . chunksOf 2 . splitWhen ((== Just Markup) . aspect . trd)
else map mkHtml
where
trd (_, _, a) = a
mkHtml :: (Int, String, Aspects) -> Html
mkHtml (pos, s, mi) =
-- Andreas, 2017-06-16, issue #2605:
-- Do not create anchors for whitespace.
applyUnless (mi == mempty) (annotate pos mi) $ toHtml s
mkMd :: [[(Int, String, Aspects)]] -> Html
mkMd = go
where
work a@(pos, s, mi) = case aspect mi of
Just Background -> preEscapedToHtml s
Just Markup -> __IMPOSSIBLE__
_ -> mkHtml a
go [a, b] = mconcat [mconcat $ work <$> a, pre ! class_ "agda-code" $ mconcat $ work <$> b]
go [a] = mconcat $ work <$> a
go _ = __IMPOSSIBLE__

@ice1000 ice1000 added the html-backend label Nov 7, 2018

ice1000 added a commit to Agda-zh/agda that referenced this issue Nov 13, 2018

[ close agda#3373 ] Implementation, basic test case
Signed-off-by: ice1000 <ice1000kotlin@foxmail.com>

ice1000 added a commit to Agda-zh/agda that referenced this issue Nov 13, 2018

[ agda#3373 ] Add auto test, generate `.rst` files when `--html-highl…
…ight=auto`

Signed-off-by: ice1000 <ice1000kotlin@foxmail.com>

ice1000 added a commit to Agda-zh/agda that referenced this issue Nov 13, 2018

[ agda#3373 ] Add auto test, generate `.rst` files when `--html-highl…
…ight=auto`

Signed-off-by: ice1000 <ice1000kotlin@foxmail.com>

@ice1000 ice1000 added this to the 2.6.0 milestone Nov 13, 2018

@ice1000 ice1000 self-assigned this Nov 13, 2018

ice1000 added a commit to Agda-zh/agda that referenced this issue Nov 13, 2018

[ agda#3373 ] Move functions, document the change
[ci skip]

Signed-off-by: ice1000 <ice1000kotlin@foxmail.com>

ice1000 added a commit to Agda-zh/agda that referenced this issue Nov 13, 2018

[ close agda#3373 ] Implementation, basic test case
Signed-off-by: ice1000 <ice1000kotlin@foxmail.com>

ice1000 added a commit to Agda-zh/agda that referenced this issue Nov 13, 2018

[ agda#3373 ] Add auto test, generate `.rst` files when `--html-highl…
…ight=auto`

Signed-off-by: ice1000 <ice1000kotlin@foxmail.com>

ice1000 added a commit to Agda-zh/agda that referenced this issue Nov 13, 2018

[ agda#3373 ] Move functions, document the change
Signed-off-by: ice1000 <ice1000kotlin@foxmail.com>

@ice1000 ice1000 closed this in 2d98089 Nov 13, 2018

@ice1000 ice1000 changed the title from Wrap HTML in `raw` directive when working with reStructuredText to Wrap HTML in `raw` directive when working with ReStructuredText Nov 13, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment