git.iko.soy/typeable/pandoc
on master help sign in
Org reader: undo code duplication

Some code was duplicated (copy-pasted) or placed in an inappropriate module during the modularization refactoring. Those functions are moved into a `Shared` module, as was originally intended but forgotten. Better documentation of the respective functions is a positive side-effect.

commit512bf2eebf5d59916b1154ef2025d776b367c035 authorAlbert Krewinkel <albert@zeitkraut.de> dateMon 30 May 2016, 15:07 (10 years ago) parent061bc60
4 files changed+84−55
pandoc.cabal+1 src/Text/Pandoc/Readers/Org/Blocks.hs+3−21 src/Text/Pandoc/Readers/Org/Inlines.hs+4−34 src/Text/Pandoc/Readers/Org/Shared.hs+76
pandoc.cabal+1
@@ -398,6 +398,7 @@ Library
398398 Text.Pandoc.Readers.Org.Inlines,
399399 Text.Pandoc.Readers.Org.ParserState,
400400 Text.Pandoc.Readers.Org.Parsing,
401+ Text.Pandoc.Readers.Org.Shared,
401402 Text.Pandoc.Writers.Shared,
402403 Text.Pandoc.Asciify,
403404 Text.Pandoc.MIME,
src/Text/Pandoc/Readers/Org/Blocks.hs+3−21
@@ -35,6 +35,9 @@ import Text.Pandoc.Readers.Org.BlockStarts
3535import Text.Pandoc.Readers.Org.Inlines
3636import Text.Pandoc.Readers.Org.ParserState
3737import Text.Pandoc.Readers.Org.Parsing
38+import Text.Pandoc.Readers.Org.Shared
39+ ( isImageFilename, rundocBlockClass, toRundocAttrib
40+ , translateLang )
3841
3942import qualified Text.Pandoc.Builder as B
4043import Text.Pandoc.Builder ( Inlines, Blocks )
@@ -43,7 +46,6 @@ import Text.Pandoc.Compat.Monoid ((<>))
4346import Text.Pandoc.Options
4447import Text.Pandoc.Shared ( compactify', compactify'DL )
4548
46−import Control.Arrow ( first )
4749import Control.Monad ( foldM, guard, mzero )
4850import Data.Char ( isSpace, toLower, toUpper)
4951import Data.List ( foldl', intersperse, isPrefixOf )
@@ -314,7 +316,6 @@ codeHeaderArgs = try $ do
314316 else ([ pandocLang ], parameters)
315317 where
316318 hasRundocParameters = not . null
317− toRundocAttrib = first ("rundoc-" ++)
318319
319320switch :: OrgParser (Char, Maybe String)
320321switch = try $ simpleSwitch <|> lineNumbersSwitch
@@ -323,25 +324,6 @@ switch = try $ simpleSwitch <|> lineNumbersSwitch
323324 lineNumbersSwitch = (\ls -> ('l', Just ls)) <$>
324325 (string "-l \"" *> many1Till nonspaceChar (char '"'))
325326
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−
345327blockOption :: OrgParser (String, String)
346328blockOption = try $ do
347329 argKey <- orgArgKey
src/Text/Pandoc/Readers/Org/Inlines.hs+4−34
@@ -30,13 +30,15 @@ module Text.Pandoc.Readers.Org.Inlines
3030 ( inline
3131 , inlines
3232 , addToNotesTable
33− , isImageFilename
3433 , linkTarget
3534 ) where
3635
3736import Text.Pandoc.Readers.Org.BlockStarts
3837import Text.Pandoc.Readers.Org.ParserState
3938import Text.Pandoc.Readers.Org.Parsing
39+import Text.Pandoc.Readers.Org.Shared
40+ ( isImageFilename, rundocBlockClass, toRundocAttrib
41+ , translateLang )
4042
4143import qualified Text.Pandoc.Builder as B
4244import Text.Pandoc.Builder ( Inlines )
@@ -47,35 +49,12 @@ import Text.Pandoc.Readers.LaTeX ( inlineCommand, rawLaTeXInline )
4749import Text.TeXMath ( readTeX, writePandoc, DisplayType(..) )
4850import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
4951
50−import Control.Arrow ( first )
5152import Control.Monad ( guard, mplus, mzero, when )
5253import Data.Char ( isAlphaNum, isSpace )
53−import Data.List ( isPrefixOf, isSuffixOf )
54+import Data.List ( isPrefixOf )
5455import Data.Maybe ( fromMaybe )
5556import qualified Data.Map as M
5657
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−
7958--
8059-- Functions acting on the parser state
8160--
@@ -405,15 +384,6 @@ cleanLinkString s =
405384 in all (\c -> isAlphaNum c || c `elem` (".-"::String)) scheme
406385 && not (null path)
407386
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−
417387internalLink :: String -> Inlines -> F Inlines
418388internalLink link title = do
419389 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