Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 146 lines (133 sloc) 5.572 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
{-
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 translating between Page structures and raw
- text strings. The strings may begin with a metadata block,
- which looks like this (it is valid YAML):
-
- > ---
- > title: Custom Title
- > format: markdown+lhs
- > toc: yes
- > categories: foo bar baz
- > ...
-
- This would tell gitit to use "Custom Title" as the displayed
- page title (instead of the page name), to interpret the page
- text as markdown with literate haskell, to include a table of
- contents, and to include the page in the categories foo, bar,
- and baz.
-
- The metadata block may be omitted entirely, and any particular line
- may be omitted. The categories in the @categories@ field should be
- separated by spaces. Commas will be treated as spaces.
-
- Metadata value fields may be continued on the next line, as long as
- it is nonblank and starts with a space character.
-
- Unrecognized metadata fields are simply ignored.
-}

module Network.Gitit.Page ( stringToPage
                          , pageToString
                          , extractCategories
                          )
where
import Network.Gitit.Types
import Network.Gitit.Util (trim, splitCategories, parsePageType)
import Text.ParserCombinators.Parsec
import Data.Char (toLower)
import Data.List (intercalate)
import Data.Maybe (fromMaybe)

parseMetadata :: String -> ([(String, String)], String)
parseMetadata raw =
  case parse pMetadataBlock "" raw of
    Left _ -> ([], raw)
    Right (ls, rest) -> (ls, rest)

pMetadataBlock :: GenParser Char st ([(String, String)], String)
pMetadataBlock = try $ do
  string "---"
  pBlankline
  ls <- many pMetadataLine
  string "..."
  pBlankline
  skipMany pBlankline
  rest <- getInput
  return (ls, rest)

pBlankline :: GenParser Char st Char
pBlankline = try $ many (oneOf " \t") >> newline

pMetadataLine :: GenParser Char st (String, String)
pMetadataLine = try $ do
  ident <- many1 letter
  skipMany (oneOf " \t")
  char ':'
  rawval <- many $ noneOf "\n\r"
                 <|> (try $ newline >> notFollowedBy pBlankline >>
                            skipMany1 (oneOf " \t") >> return ' ')
  newline
  return (ident, trim rawval)

-- | Read a string (the contents of a page file) and produce a Page
-- object, using defaults except when overridden by metadata.
stringToPage :: Config -> String -> String -> Page
stringToPage conf pagename raw =
  let (ls, rest) = parseMetadata raw
      page' = Page { pageName = pagename
                   , pageFormat = defaultPageType conf
                   , pageLHS = defaultLHS conf
                   , pageTOC = tableOfContents conf
                   , pageTitle = pagename
                   , pageCategories = []
                   , pageText = filter (/= '\r') rest
                   , pageMeta = ls }
  in foldr adjustPage page' ls

adjustPage :: (String, String) -> Page -> Page
adjustPage ("title", val) page' = page' { pageTitle = val }
adjustPage ("format", val) page' = page' { pageFormat = pt, pageLHS = lhs }
    where (pt, lhs) = parsePageType val
adjustPage ("toc", val) page' = page' {
  pageTOC = (map toLower val) `elem` ["yes","true"] }
adjustPage ("categories", val) page' =
   page' { pageCategories = splitCategories val ++ pageCategories page' }
adjustPage (_, _) page' = page'

-- | Write a string (the contents of a page file) corresponding to
-- a Page object, using explicit metadata only when needed.
pageToString :: Config -> Page -> String
pageToString conf page' =
  let pagename = pageName page'
      pagetitle = pageTitle page'
      pageformat = pageFormat page'
      pagelhs = pageLHS page'
      pagetoc = pageTOC page'
      pagecats = pageCategories page'
      metadata' = (if pagename /= pagetitle
                       then "!title: " ++ pagetitle ++ "\n"
                       else "") ++
                   (if pageformat /= defaultPageType conf ||
                          pagelhs /= defaultLHS conf
                       then "!format: " ++
                            map toLower (show pageformat) ++
                            if pagelhs then "+lhs\n" else "\n"
                       else "") ++
                   (if pagetoc /= tableOfContents conf
                       then "!toc: " ++
                            (if pagetoc then "yes" else "no") ++ "\n"
                       else "") ++
                   (if not (null pagecats)
                       then "!categories: " ++ intercalate " " pagecats ++ "\n"
                       else "")
  in metadata' ++ (if null metadata' then "" else "\n") ++ pageText page'

extractCategories :: String -> [String]
extractCategories s | take 3 s == "---" =
  let (md,_) = parseMetadata s
  in splitCategories $ fromMaybe "" $ lookup "categories" md
extractCategories _ = []
Something went wrong with that request. Please try again.