Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ebobrow
Copy link
Contributor

@ebobrow ebobrow commented Jun 15, 2022

parse \texttt{\textbf{bold code}} as Strong [Code ("",[],[]) "bold code"]

closes #7525

@ebobrow
Copy link
Contributor Author

ebobrow commented Jun 16, 2022

Thanks for the review! I moved the function to Shared, but I couldn't find the other reader you were talking about.

@ebobrow ebobrow force-pushed the 7525 branch 4 times, most recently from 1a56301 to 03458fd Compare June 19, 2022 21:56
@ebobrow
Copy link
Contributor Author

ebobrow commented Jun 19, 2022

Sorry for all the force-pushes. I've cleaned some stuff up and I tihnk it's looking better now.

@jgm
Copy link
Owner

jgm commented Jun 19, 2022

I want to suggest these changes, which I think clarify the code even further, and also give us the flexibility to add attributes to the Code elements.

diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 3f34e0b8a..988cc3055 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -347,7 +347,7 @@ inlineCommands = M.unions
     , ("textmd", extractSpaces (spanWith ("",["medium"],[])) <$> tok)
     , ("textrm", extractSpaces (spanWith ("",["roman"],[])) <$> tok)
     , ("textup", extractSpaces (spanWith ("",["upright"],[])) <$> tok)
-    , ("texttt", formatCode <$> tok)
+    , ("texttt", formatCode nullAttr <$> tok)
     , ("sout", extractSpaces strikeout <$> tok)
     , ("alert", skipopts >> spanWith ("",["alert"],[]) <$> tok) -- beamer
     , ("textsuperscript", extractSpaces superscript <$> tok)
@@ -368,7 +368,7 @@ inlineCommands = M.unions
     , ("it", extractSpaces emph <$> inlines)
     , ("sl", extractSpaces emph <$> inlines)
     , ("bf", extractSpaces strong <$> inlines)
-    , ("tt", formatCode <$> inlines)
+    , ("tt", formatCode nullAttr <$> inlines)
     , ("rm", inlines)
     , ("itshape", extractSpaces emph <$> inlines)
     , ("slshape", extractSpaces emph <$> inlines)
@@ -407,8 +407,8 @@ inlineCommands = M.unions
     , ("hypertarget", hypertargetInline)
     -- hyphenat
     , ("nohyphens", tok)
-    , ("textnhtt", formatCode <$> tok)
-    , ("nhttfamily", formatCode <$> tok)
+    , ("textnhtt", formatCode nullAttr <$> tok)
+    , ("nhttfamily", formatCode nullAttr <$> tok)
     -- LaTeX colors
     , ("textcolor", coloredInline "color")
     , ("colorbox", coloredInline "background-color")
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index 0d6a1b559..d6d24e4e6 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -780,13 +780,14 @@ filterIpynbOutput mode = walk go
                     | otherwise = ""
         go x = x
 
--- | Move formatting within code inlines to the outside,
--- i.e. `Strong [ Code ("",[],[]) "Strong code" ]` (issue #7525)
-formatCode :: Inlines -> Inlines
-formatCode =
-  B.fromList
-    . fmt
-    . B.toList
+-- | Reformat 'Inlines' as code, puting the stringlike parts in 'Code'
+-- elements while bringing other inline formatting outside.
+-- The idea is that e.g. `[Str "a",Space,Strong [Str "b"]]` should turn
+-- into `[Code ("",[],[]) "a ", Strong [Code ("",[],[]) "b"]]`.
+-- This helps work around the limitation that pandoc's Code element can
+-- only contain string content (see issue #7525).
+formatCode :: Attr -> Inlines -> Inlines
+formatCode attr = B.fromList . fmt . walk fmt . B.toList
   where
     isPlaintext (Str _) = True
     isPlaintext Space = True
@@ -796,13 +797,8 @@ formatCode =
     fmt = concatMap go . groupBy (\a b -> isPlaintext a && isPlaintext b)
       where
         go xs
-          | all isPlaintext xs = B.toList $ B.code $ stringify xs
-          | otherwise = walk
-                            ( \case
-                                  [y] | not (isPlaintext y) -> [y]
-                                  ys -> fmt ys
-                            )
-                            xs
+          | all isPlaintext xs = B.toList $ B.codeWith attr $ stringify xs
+          | otherwise = xs
 
 --
 -- TagSoup HTML handling

@ebobrow
Copy link
Contributor Author

ebobrow commented Jun 20, 2022

Done, thanks! Unfortunately I can't remove walk from the otherwise branch because it stops code from being applied to the innermost Inlines.

@jgm
Copy link
Owner

jgm commented Jun 20, 2022

Unfortunately I can't remove walk from the otherwise branch because it stops code from being applied to the innermost Inlines.

I don't think that's true! Remember, walk is a bottom-up traversal, so in walk fmt fmt will be applied to the innermost inline lists first. The above code passed all your tests -- do you have a test case it doesn't pass?

@ebobrow
Copy link
Contributor Author

ebobrow commented Jun 20, 2022

I don't think that's true!

You're right, my apologies! I missed the walk fmt in the top-level definition the first time around, so it was failing tests. Thank you for explaining and again I'm very sorry!

@jgm
Copy link
Owner

jgm commented Jun 20, 2022

Looks good except for a small typo in the comment.
I think that after this is merged, it would be good to use formatCode in some of the other readers (esp. HTML), but we can do that separately.

@ebobrow
Copy link
Contributor Author

ebobrow commented Jun 20, 2022

Perfect, thanks for all your help! I can work on a second PR after this is merged.

add `formatCode` function to Shared to parse e.g.
`\texttt{\textbf{bold code}}` as `Strong [Code ("",[],[]) "bold code"]`
@jgm jgm merged commit f317ec4 into jgm:master Jun 20, 2022
@jgm
Copy link
Owner

jgm commented Jun 20, 2022

Great, thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Latex: Bold inside tt fails, tt inside bold works

2 participants