git.iko.soy/typeable/pandoc
on master help sign in
Org reader: undo code duplication
512bf2eAlbert Krewinkel
pandoc.cabal+1
@@ -398,6 +398,7 @@ Library
398 Text.Pandoc.Readers.Org.Inlines,
398 Text.Pandoc.Readers.Org.Inlines,
399 Text.Pandoc.Readers.Org.ParserState,
399 Text.Pandoc.Readers.Org.ParserState,
400 Text.Pandoc.Readers.Org.Parsing,
400 Text.Pandoc.Readers.Org.Parsing,
401+ Text.Pandoc.Readers.Org.Shared,
401 Text.Pandoc.Writers.Shared,
402 Text.Pandoc.Writers.Shared,
402 Text.Pandoc.Asciify,
403 Text.Pandoc.Asciify,
403 Text.Pandoc.MIME,
404 Text.Pandoc.MIME,
src/Text/Pandoc/Readers/Org/Blocks.hs+3−21
@@ -35,6 +35,9 @@ import Text.Pandoc.Readers.Org.BlockStarts
35import Text.Pandoc.Readers.Org.Inlines
35import Text.Pandoc.Readers.Org.Inlines
36import Text.Pandoc.Readers.Org.ParserState
36import Text.Pandoc.Readers.Org.ParserState
37import Text.Pandoc.Readers.Org.Parsing
37import Text.Pandoc.Readers.Org.Parsing
38+import Text.Pandoc.Readers.Org.Shared
39+ ( isImageFilename, rundocBlockClass, toRundocAttrib
40+ , translateLang )
38
41
39import qualified Text.Pandoc.Builder as B
42import qualified Text.Pandoc.Builder as B
40import Text.Pandoc.Builder ( Inlines, Blocks )
43import Text.Pandoc.Builder ( Inlines, Blocks )
@@ -43,7 +46,6 @@ import Text.Pandoc.Compat.Monoid ((<>))
43import Text.Pandoc.Options
46import Text.Pandoc.Options
44import Text.Pandoc.Shared ( compactify', compactify'DL )
47import Text.Pandoc.Shared ( compactify', compactify'DL )
45
48
46−import Control.Arrow ( first )
47import Control.Monad ( foldM, guard, mzero )
49import Control.Monad ( foldM, guard, mzero )
48import Data.Char ( isSpace, toLower, toUpper)
50import Data.Char ( isSpace, toLower, toUpper)
49import Data.List ( foldl', intersperse, isPrefixOf )
51import Data.List ( foldl', intersperse, isPrefixOf )
@@ -314,7 +316,6 @@ codeHeaderArgs = try $ do
314 else ([ pandocLang ], parameters)
316 else ([ pandocLang ], parameters)
315 where
317 where
316 hasRundocParameters = not . null
318 hasRundocParameters = not . null
317− toRundocAttrib = first ("rundoc-" ++)
318
319
319switch :: OrgParser (Char, Maybe String)
320switch :: OrgParser (Char, Maybe String)
320switch = try $ simpleSwitch <|> lineNumbersSwitch
321switch = try $ simpleSwitch <|> lineNumbersSwitch
@@ -323,25 +324,6 @@ switch = try $ simpleSwitch <|> lineNumbersSwitch
323 lineNumbersSwitch = (\ls -> ('l', Just ls)) <$>
324 lineNumbersSwitch = (\ls -> ('l', Just ls)) <$>
324 (string "-l \"" *> many1Till nonspaceChar (char '"'))
325 (string "-l \"" *> many1Till nonspaceChar (char '"'))
325
326
326−translateLang :: String -> String
327−translateLang "C" = "c"
328−translateLang "C++" = "cpp"
329−translateLang "emacs-lisp" = "commonlisp" -- emacs lisp is not supported
330−translateLang "js" = "javascript"
331−translateLang "lisp" = "commonlisp"
332−translateLang "R" = "r"
333−translateLang "sh" = "bash"
334−translateLang "sqlite" = "sql"
335−translateLang cs = cs
336−
337−-- | Prefix used for Rundoc classes and arguments.
338−rundocPrefix :: String
339−rundocPrefix = "rundoc-"
340−
341−-- | The class-name used to mark rundoc blocks.
342−rundocBlockClass :: String
343−rundocBlockClass = rundocPrefix ++ "block"
344−
345blockOption :: OrgParser (String, String)
327blockOption :: OrgParser (String, String)
346blockOption = try $ do
328blockOption = try $ do
347 argKey <- orgArgKey
329 argKey <- orgArgKey
src/Text/Pandoc/Readers/Org/Inlines.hs+4−34
@@ -30,13 +30,15 @@ module Text.Pandoc.Readers.Org.Inlines
30 ( inline
30 ( inline
31 , inlines
31 , inlines
32 , addToNotesTable
32 , addToNotesTable
33− , isImageFilename
34 , linkTarget
33 , linkTarget
35 ) where
34 ) where
36
35
37import Text.Pandoc.Readers.Org.BlockStarts
36import Text.Pandoc.Readers.Org.BlockStarts
38import Text.Pandoc.Readers.Org.ParserState
37import Text.Pandoc.Readers.Org.ParserState
39import Text.Pandoc.Readers.Org.Parsing
38import Text.Pandoc.Readers.Org.Parsing
39+import Text.Pandoc.Readers.Org.Shared
40+ ( isImageFilename, rundocBlockClass, toRundocAttrib
41+ , translateLang )
40
42
41import qualified Text.Pandoc.Builder as B
43import qualified Text.Pandoc.Builder as B
42import Text.Pandoc.Builder ( Inlines )
44import Text.Pandoc.Builder ( Inlines )
@@ -47,35 +49,12 @@ import Text.Pandoc.Readers.LaTeX ( inlineCommand, rawLaTeXInline )
47import Text.TeXMath ( readTeX, writePandoc, DisplayType(..) )
49import Text.TeXMath ( readTeX, writePandoc, DisplayType(..) )
48import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
50import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
49
51
50−import Control.Arrow ( first )
51import Control.Monad ( guard, mplus, mzero, when )
52import Control.Monad ( guard, mplus, mzero, when )
52import Data.Char ( isAlphaNum, isSpace )
53import Data.Char ( isAlphaNum, isSpace )
53−import Data.List ( isPrefixOf, isSuffixOf )
54+import Data.List ( isPrefixOf )
54import Data.Maybe ( fromMaybe )
55import Data.Maybe ( fromMaybe )
55import qualified Data.Map as M
56import qualified Data.Map as M
56
57
57−-- | Prefix used for Rundoc classes and arguments.
58−rundocPrefix :: String
59−rundocPrefix = "rundoc-"
60−
61−-- | The class-name used to mark rundoc blocks.
62−rundocBlockClass :: String
63−rundocBlockClass = rundocPrefix ++ "block"
64−
65−toRundocAttrib :: (String, String) -> (String, String)
66−toRundocAttrib = first ("rundoc-" ++)
67−
68−translateLang :: String -> String
69−translateLang "C" = "c"
70−translateLang "C++" = "cpp"
71−translateLang "emacs-lisp" = "commonlisp" -- emacs lisp is not supported
72−translateLang "js" = "javascript"
73−translateLang "lisp" = "commonlisp"
74−translateLang "R" = "r"
75−translateLang "sh" = "bash"
76−translateLang "sqlite" = "sql"
77−translateLang cs = cs
78−
79--
58--
80-- Functions acting on the parser state
59-- Functions acting on the parser state
81--
60--
@@ -405,15 +384,6 @@ cleanLinkString s =
405 in all (\c -> isAlphaNum c || c `elem` (".-"::String)) scheme
384 in all (\c -> isAlphaNum c || c `elem` (".-"::String)) scheme
406 && not (null path)
385 && not (null path)
407
386
408−isImageFilename :: String -> Bool
409−isImageFilename filename =
410− any (\x -> ('.':x) `isSuffixOf` filename) imageExtensions &&
411− (any (\x -> (x++":") `isPrefixOf` filename) protocols ||
412− ':' `notElem` filename)
413− where
414− imageExtensions = [ "jpeg" , "jpg" , "png" , "gif" , "svg" ]
415− protocols = [ "file", "http", "https" ]
416−
417internalLink :: String -> Inlines -> F Inlines
387internalLink :: String -> Inlines -> F Inlines
418internalLink link title = do
388internalLink link title = do
419 anchorB <- (link `elem`) <$> asksF orgStateAnchorIds
389 anchorB <- (link `elem`) <$> asksF orgStateAnchorIds
src/Text/Pandoc/Readers/Org/Shared.hsnew file+76
@@ -0,0 +1,76 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-
3+Copyright (C) 2014-2016 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
4+
5+This program is free software; you can redistribute it and/or modify
6+it under the terms of the GNU General Public License as published by
7+the Free Software Foundation; either version 2 of the License, or
8+(at your option) any later version.
9+
10+This program is distributed in the hope that it will be useful,
11+but WITHOUT ANY WARRANTY; without even the implied warranty of
12+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+GNU General Public License for more details.
14+
15+You should have received a copy of the GNU General Public License
16+along with this program; if not, write to the Free Software
17+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+-}
19+
20+{- |
21+ Module : Text.Pandoc.Readers.Org.Options
22+ Copyright : Copyright (C) 2014-2016 Albert Krewinkel
23+ License : GNU GPL, version 2 or above
24+
25+ Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
26+
27+Utility functions used in other Pandoc Org modules.
28+-}
29+module Text.Pandoc.Readers.Org.Shared
30+ ( isImageFilename
31+ , rundocBlockClass
32+ , toRundocAttrib
33+ , translateLang
34+ ) where
35+
36+import Control.Arrow ( first )
37+import Data.List ( isPrefixOf, isSuffixOf )
38+
39+
40+-- | Check whether the given string looks like the path to of URL of an image.
41+isImageFilename :: String -> Bool
42+isImageFilename filename =
43+ any (\x -> ('.':x) `isSuffixOf` filename) imageExtensions &&
44+ (any (\x -> (x++":") `isPrefixOf` filename) protocols ||
45+ ':' `notElem` filename)
46+ where
47+ imageExtensions = [ "jpeg" , "jpg" , "png" , "gif" , "svg" ]
48+ protocols = [ "file", "http", "https" ]
49+
50+-- | Prefix used for Rundoc classes and arguments.
51+rundocPrefix :: String
52+rundocPrefix = "rundoc-"
53+
54+-- | The class-name used to mark rundoc blocks.
55+rundocBlockClass :: String
56+rundocBlockClass = rundocPrefix ++ "block"
57+
58+-- | Prefix the name of a attribute, marking it as a code execution parameter.
59+toRundocAttrib :: (String, String) -> (String, String)
60+toRundocAttrib = first (rundocPrefix ++)
61+
62+-- | Translate from Org-mode's programming language identifiers to those used
63+-- by Pandoc. This is useful to allow for proper syntax highlighting in
64+-- Pandoc output.
65+translateLang :: String -> String
66+translateLang cs =
67+ case cs of
68+ "C" -> "c"
69+ "C++" -> "cpp"
70+ "emacs-lisp" -> "commonlisp" -- emacs lisp is not supported
71+ "js" -> "javascript"
72+ "lisp" -> "commonlisp"
73+ "R" -> "r"
74+ "sh" -> "bash"
75+ "sqlite" -> "sql"
76+ _ -> cs