Skip to content
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

lstinline with braces can be used (verb cannot be used with braces) #3535

Merged
merged 4 commits into from
Mar 29, 2017
Merged

lstinline with braces can be used (verb cannot be used with braces) #3535

merged 4 commits into from
Mar 29, 2017

Conversation

schrieveslaach
Copy link
Contributor

I want to resolve #3534

@jgm
Copy link
Owner

jgm commented Mar 28, 2017

Great! I think we can make this better, though.

Some thoughts:

  • With the braced form, there can be escaped braces, right?
  • We should handle the language attribute. To do this, you can parse options with option [] keyvals -- this will give you a [(String, String)] association list. Then, to construct the sort of attributes pandoc uses for language, and convert from listings language names to pandoc names:
  return $ codeWith ("",classes,[]) s
            where classes = case lookup "language" opts >>= fromListingsLanguage of
                               Just l  -> [l]
                               Nothing -> []

or simplify that using maybeToList from Data.Maybe.

@schrieveslaach
Copy link
Contributor Author

With the braced form, there can be escaped braces, right?

I think it is not supported. In this case, you would just use different characters than braces.

We should handle the language attribute. To do this, you can parse options with option [] keyvals -- this will give you a [(String, String)] association list. Then, to construct the sort of attributes pandoc uses for language, and convert from listings language names to pandoc names...

I will give it a try.

@jgm
Copy link
Owner

jgm commented Mar 28, 2017 via email

@schrieveslaach
Copy link
Contributor Author

@jgm, I need some help. My changes won't compile:

diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index d632eb2d..142f2c04 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -601,7 +601,8 @@ inlineCommands = M.fromList $
   , ("thanks", note <$> grouped block)
   , ("footnote", note <$> grouped block)
   , ("verb", doverb)
-  , ("lstinline", skipopts *> dolstinline)
+  , ("lstinline", do options <- option [] keyvals
+                     dolstinline options)
   , ("Verb", doverb)
   , ("url", (unescapeURL <$> braced) >>= \url ->
        pure (link url "" (str url)))
@@ -716,10 +717,14 @@ doverb = do
   marker <- anyChar
   code <$> manyTill (satisfy (/='\n')) (char marker)
 
-dolstinline :: PandocMonad m => LP m Inlines
-dolstinline = do
+dolstinline :: PandocMonad m => [(String, String)] -> LP m Inlines
+dolstinline opts = do
   marker <- char '{' <|> anyChar 
   code <$> manyTill (satisfy (/='\n')) (char '}' <|> char marker)
+  return $ codeWith ("",classes,[]) code
+           where classes = case lookup "language" opts >>= fromListingsLanguage of
+                               Just l  -> [l]
+                               Nothing -> []
 
 doLHSverb :: PandocMonad m => LP m Inlines
 doLHSverb = codeWith ("",["haskell"],[]) <$> manyTill (satisfy (/='\n')) (char '|')

Here's the compiler error:

src/Text/Pandoc/Readers/LaTeX.hs:724:37: error:
    • Couldn't match type ‘String -> Inlines’ with ‘[Char]’
      Expected type: String
        Actual type: String -> Inlines
    • Probable cause: ‘code’ is applied to too few arguments
      In the second argument of ‘codeWith’, namely ‘code’
      In the second argument of ‘($)’, namely
        ‘codeWith ("", classes, []) code’
      In a stmt of a 'do' block: return $ codeWith ("", classes, []) code

I'm pretty sure that I'm using your code snippet wrong.

@schrieveslaach
Copy link
Contributor Author

@jgm, nevermind. I've found the solution.

@@ -601,7 +601,8 @@ inlineCommands = M.fromList $
, ("thanks", note <$> grouped block)
, ("footnote", note <$> grouped block)
, ("verb", doverb)
, ("lstinline", skipopts *> dolstinline)
, ("lstinline", do options <- option [] keyvals
Copy link
Owner

@jgm jgm Mar 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this cleaner, move options <- option [] keyvals down into dolstinline and remove the opts parameter there (and here).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to move, but I get an compiler error Variable not in scope: options :: [([Char], String)]

diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 2e40e8eb..11d64340 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -601,8 +601,7 @@ inlineCommands = M.fromList $
   , ("thanks", note <$> grouped block)
   , ("footnote", note <$> grouped block)
   , ("verb", doverb)
-  , ("lstinline", do options <- option [] keyvals
-                     dolstinline options)
+  , ("lstinline", dolstinline)
   , ("Verb", doverb)
   , ("url", (unescapeURL <$> braced) >>= \url ->
        pure (link url "" (str url)))
@@ -717,11 +716,12 @@ doverb = do
   marker <- anyChar
   code <$> manyTill (satisfy (/='\n')) (char marker)
 
-dolstinline :: PandocMonad m => [(String, String)] -> LP m Inlines
-dolstinline opts = do
+dolstinline :: PandocMonad m => LP m Inlines
+dolstinline = do
+  options <- option [] keyvals
   marker <- char '{' <|> anyChar 
   codeWith ("",classes,[]) <$> manyTill (satisfy (/='\n')) (char '}' <|> char marker)
-               where classes = case lookup "language" opts >>= fromListingsLanguage of
+               where classes = case lookup "language" options >>= fromListingsLanguage of
                                Just l  -> [l]
                                Nothing -> []

Other function using options <- option [] keyvals have LP m Blocks in there signature. Does this matter?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there are some unexpected things about scope with where.

Try this instead:

  options <- option [] keyvals
  let classes = maybeToList $ lookup "language" options >>= fromListingsLanguage
  marker <- char '{' <|> anyChar
  codeWith ...as before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🥇 Done and PR has been updated.

@jgm jgm merged commit 5fe734d into jgm:master Mar 29, 2017
@jgm
Copy link
Owner

jgm commented Mar 29, 2017

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.

Lstinline with Braces Does Not Work
2 participants