Browse files

Added Gitit.Convert.

  • Loading branch information...
1 parent 7bf135e commit d639e2c573e1a86f366bad85422ca00972035435 @jgm committed Jan 30, 2009
Showing with 84 additions and 52 deletions.
  1. +12 −51 Gitit.hs
  2. +71 −0 Gitit/Convert.hs
  3. +1 −1 gitit.cabal
View
63 Gitit.hs
@@ -24,6 +24,7 @@ import Gitit.HAppS
import Gitit.Util (orIfNull, consolidateHeads)
import Gitit.Initialize (createStaticIfMissing, createRepoIfMissing)
import Gitit.Framework
+import Gitit.Convert
import Gitit.Export (exportFormats)
import System.IO.UTF8
import System.IO (stderr)
@@ -44,8 +45,7 @@ import qualified Data.Map as M
import Data.Ord (comparing)
import Paths_gitit
import Text.Pandoc
-import Text.Pandoc.Definition (processPandoc)
-import Text.Pandoc.Shared (HTMLMathMethod(..), substitute)
+import Text.Pandoc.Shared (substitute)
import Data.Char (isAlphaNum, isAlpha, toLower)
import Control.Monad.Reader
import qualified Data.ByteString.Lazy as B
@@ -553,32 +553,6 @@ fileListToHtml prefix lst = ulist ! [identifier "index", theclass "folding"] <<
else li ! [theclass "folder"] << [stringToHtml h', fileListToHtml (prefix ++ h') l]) $
consolidateHeads lst)
--- | Convert links with no URL to wikilinks.
-convertWikiLinks :: Inline -> Inline
-convertWikiLinks (Link ref ("", "")) =
- Link ref (refToUrl ref, "Go to wiki page")
-convertWikiLinks x = x
-
-refToUrl :: [Inline] -> String
-refToUrl = concatMap go
- where go (Str x) = x
- go (Space) = "%20"
- go (Quoted DoubleQuote xs) = '"' : (refToUrl xs ++ "\"")
- go (Quoted SingleQuote xs) = '\'' : (refToUrl xs ++ "'")
- go (Apostrophe) = "'"
- go (Ellipses) = "..."
- go (Math InlineMath t) = '$' : (t ++ "$")
- go _ = ""
-
--- | Converts pandoc document to HTML.
-pandocToHtml :: MonadIO m => Pandoc -> m Html
-pandocToHtml pandocContents = do
- cfg <- getConfig
- return $ writeHtml (defaultWriterOptions { writerStandalone = False
- , writerHTMLMathMethod = JsMath (Just "/js/jsMath/easy/load.js")
- , writerTableOfContents = tableOfContents cfg
- }) $ processPandoc convertWikiLinks pandocContents
-
-- | Abstract representation of page layout (tabs, scripts, etc.)
data PageLayout = PageLayout
{ pgTitle :: String
@@ -826,26 +800,22 @@ exportBox page params | isPage page =
, submit "export" "Export" ])
exportBox _ _ = noHtml
+exportPage :: String -> Params -> Web Response
+exportPage page params = do
+ let format = pFormat params
+ mDoc <- pageAsPandoc page params
+ case mDoc of
+ Nothing -> error $ "Unable to retrieve page contents."
+ Just doc -> case lookup format exportFormats of
+ Nothing -> error $ "Unknown export format: " ++ format
+ Just writer -> writer page doc
+
rawContents :: String -> Params -> Web (Maybe String)
rawContents file params = do
let rev = pRevision params
fs <- getFileStore
liftIO $ catch (retrieve fs file rev >>= return . Just) (\e -> if e == NotFound then return Nothing else throwIO e)
-{-
-removeRawHtmlBlock :: Block -> Block
-removeRawHtmlBlock (RawHtml _) = RawHtml "<!-- raw HTML removed -->"
-removeRawHtmlBlock x = x
--}
-
-readerFor :: PageType -> (String -> Pandoc)
-readerFor pt = case pt of
- RST -> readRST (defaultParserState { stateSanitizeHTML = True, stateSmart = True })
- Markdown -> readMarkdown (defaultParserState { stateSanitizeHTML = True, stateSmart = True })
-
-textToPandoc :: PageType -> String -> Pandoc
-textToPandoc pt s = {- processPandoc removeRawHtmlBlock $ -} readerFor pt $ filter (/= '\r') s
-
pageAsPandoc :: String -> Params -> Web (Maybe Pandoc)
pageAsPandoc page params = do
pt <- getDefaultPageType
@@ -854,13 +824,4 @@ pageAsPandoc page params = do
Nothing -> Nothing
Just (Pandoc _ blocks) -> Just $ Pandoc (Meta [Str page] [] []) blocks
-exportPage :: String -> Params -> Web Response
-exportPage page params = do
- let format = pFormat params
- mDoc <- pageAsPandoc page params
- case mDoc of
- Nothing -> error $ "Unable to retrieve page contents."
- Just doc -> case lookup format exportFormats of
- Nothing -> error $ "Unknown export format: " ++ format
- Just writer -> writer page doc
View
71 Gitit/Convert.hs
@@ -0,0 +1,71 @@
+{-
+Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+-}
+
+{- Functions for converting markup formats.
+-}
+
+module Gitit.Convert ( textToPandoc
+ , pandocToHtml
+ )
+where
+import Text.Pandoc
+import Gitit.State
+import Control.Monad.Trans (MonadIO)
+import Text.XHtml
+import Text.Pandoc.Shared (HTMLMathMethod(..))
+
+{-
+removeRawHtmlBlock :: Block -> Block
+removeRawHtmlBlock (RawHtml _) = RawHtml "<!-- raw HTML removed -->"
+removeRawHtmlBlock x = x
+-}
+
+readerFor :: PageType -> (String -> Pandoc)
+readerFor pt = case pt of
+ RST -> readRST (defaultParserState { stateSanitizeHTML = True, stateSmart = True })
+ Markdown -> readMarkdown (defaultParserState { stateSanitizeHTML = True, stateSmart = True })
+
+textToPandoc :: PageType -> String -> Pandoc
+textToPandoc pt s = {- processPandoc removeRawHtmlBlock $ -} readerFor pt $ filter (/= '\r') s
+
+-- | Convert links with no URL to wikilinks.
+convertWikiLinks :: Inline -> Inline
+convertWikiLinks (Link ref ("", "")) =
+ Link ref (refToUrl ref, "Go to wiki page")
+convertWikiLinks x = x
+
+refToUrl :: [Inline] -> String
+refToUrl = concatMap go
+ where go (Str x) = x
+ go (Space) = "%20"
+ go (Quoted DoubleQuote xs) = '"' : (refToUrl xs ++ "\"")
+ go (Quoted SingleQuote xs) = '\'' : (refToUrl xs ++ "'")
+ go (Apostrophe) = "'"
+ go (Ellipses) = "..."
+ go (Math InlineMath t) = '$' : (t ++ "$")
+ go _ = ""
+
+-- | Converts pandoc document to HTML.
+pandocToHtml :: MonadIO m => Pandoc -> m Html
+pandocToHtml pandocContents = do
+ cfg <- getConfig
+ return $ writeHtml (defaultWriterOptions { writerStandalone = False
+ , writerHTMLMathMethod = JsMath (Just "/js/jsMath/easy/load.js")
+ , writerTableOfContents = tableOfContents cfg
+ }) $ processPandoc convertWikiLinks pandocContents
+
View
2 gitit.cabal
@@ -43,7 +43,7 @@ Executable gitit
hs-source-dirs: .
main-is: Gitit.hs
other-modules: Gitit.State, Gitit.HAppS, Gitit.Util, Gitit.Export,
- Gitit.Initialize, Gitit.Config, Gitit.Framework,
+ Gitit.Convert, Gitit.Initialize, Gitit.Config, Gitit.Framework,
Paths_gitit
build-depends: base >=3, parsec < 3, pretty, xhtml, containers, pandoc
>= 1.1, process, filepath, directory, mtl, cgi,

0 comments on commit d639e2c

Please sign in to comment.