From dc9c6450f3b16592d0ee865feafc17b670e4ad14 Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Wed, 20 Dec 2006 06:50:14 +0000 Subject: + Added module data for haddock. + Reformatted code consistently. git-svn-id: https://pandoc.googlecode.com/svn/trunk@252 788f1e2b-df1e-0410-8736-df70ead52e1b --- src/Text/Pandoc/Writers/HTML.hs | 311 +++++++++++++++++++++++----------------- 1 file changed, 176 insertions(+), 135 deletions(-) (limited to 'src/Text/Pandoc/Writers/HTML.hs') diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index 7ba506acb..1b5201191 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -1,4 +1,14 @@ --- | Converts Pandoc to HTML. +{- | + Module : Text.Pandoc.Writers.HTML + Copyright : Copyright (C) 2006 John MacFarlane + License : GNU GPL, version 2 or above + + Maintainer : John MacFarlane + Stability : unstable + Portability : portable + +Conversion of 'Pandoc' documents to HTML. +-} module Text.Pandoc.Writers.HTML ( writeHtml ) where @@ -13,94 +23,108 @@ import Data.List ( isPrefixOf, partition ) -- | Convert Pandoc document to string in HTML format. writeHtml :: WriterOptions -> Pandoc -> String writeHtml options (Pandoc (Meta title authors date) blocks) = - let titlePrefix = writerTitlePrefix options in - let topTitle = if not (null titlePrefix) then - [Str titlePrefix] ++ (if not (null title) then [Str " - "] ++ title else []) - else - title in - let head = if (writerStandalone options) then - htmlHeader options (Meta topTitle authors date) - else - "" - titleBlocks = if (writerStandalone options) && (not (null title)) && - (not (writerS5 options)) then - [RawHtml "

", Plain title, RawHtml "

\n"] - else - [] - foot = if (writerStandalone options) then "\n\n" else "" - blocks' = replaceReferenceLinks (titleBlocks ++ blocks) - (noteBlocks, blocks'') = partition isNoteBlock blocks' - body = (writerIncludeBefore options) ++ - concatMap (blockToHtml options) blocks'' ++ - footnoteSection options noteBlocks ++ - (writerIncludeAfter options) in - head ++ body ++ foot + let titlePrefix = writerTitlePrefix options in + let topTitle = if not (null titlePrefix) + then [Str titlePrefix] ++ (if not (null title) + then [Str " - "] ++ title + else []) + else title in + let head = if (writerStandalone options) + then htmlHeader options (Meta topTitle authors date) + else "" + titleBlocks = if (writerStandalone options) && (not (null title)) && + (not (writerS5 options)) + then [RawHtml "

", Plain title, + RawHtml "

\n"] + else [] + foot = if (writerStandalone options) then "\n\n" else "" + blocks' = replaceReferenceLinks (titleBlocks ++ blocks) + (noteBlocks, blocks'') = partition isNoteBlock blocks' + body = (writerIncludeBefore options) ++ + concatMap (blockToHtml options) blocks'' ++ + footnoteSection options noteBlocks ++ + (writerIncludeAfter options) in + head ++ body ++ foot --- | Convert list of Note blocks to a footnote
. Assumes notes are sorted. +-- | Convert list of Note blocks to a footnote
. +-- Assumes notes are sorted. footnoteSection :: WriterOptions -> [Block] -> String footnoteSection options notes = - if null notes - then "" - else "
\n
\n
    \n" ++ - concatMap (blockToHtml options) notes ++ - "
\n
\n" + if null notes + then "" + else "
\n
\n
    \n" ++ + concatMap (blockToHtml options) notes ++ + "
\n
\n" -- | Obfuscate a "mailto:" link using Javascript. obfuscateLink :: WriterOptions -> [Inline] -> String -> String obfuscateLink options text src = let text' = inlineListToHtml options text in - let linkText = if src == ("mailto:" ++ text') then "e" else "'" ++ text' ++ "'" - altText = if src == ("mailto:" ++ text') then "\\1 [at] \\2" else text' ++ " (\\1 [at] \\2)" in + let linkText = if src == ("mailto:" ++ text') + then "e" + else "'" ++ text' ++ "'" + altText = if src == ("mailto:" ++ text') + then "\\1 [at] \\2" + else text' ++ " (\\1 [at] \\2)" in gsub "mailto:([^@]*)@(.*)" ("") src -- | Obfuscate character as entity. obfuscateChar :: Char -> String -obfuscateChar char = let num = ord char in - let numstr = if even num then (show num) else ("x" ++ (showHex num "")) in - "&#" ++ numstr ++ ";" +obfuscateChar char = + let num = ord char in + let numstr = if even num then (show num) else ("x" ++ (showHex num "")) in + "&#" ++ numstr ++ ";" -- | Escape string, preserving character entities and quote. stringToHtml :: String -> String -stringToHtml str = escapePreservingRegex stringToHtmlString (mkRegex "\"|(&[[:alnum:]]*;)") str +stringToHtml str = escapePreservingRegex stringToHtmlString + (mkRegex "\"|(&[[:alnum:]]*;)") str -- | Escape string as in 'stringToHtml' but add smart typography filter. stringToSmartHtml :: String -> String stringToSmartHtml = - let escapeDoubleQuotes = - gsub "(\"|")" "”" . -- rest are right quotes - gsub "(\"|")(&r[sd]quo;)" "”\\2" . -- never left quo before right quo - gsub "(&l[sd]quo;)(\"|")" "\\2“" . -- never right quo after left quo - gsub "([ \t])(\"|")" "\\1“" . -- never right quo after space - gsub "(\"|")([^,.;:!?^) \t-])" "“\\2" . -- "word left - gsub "(\"|")('|`|‘)" "”’" . -- right if it got through last filter - gsub "(\"|")('|`|‘)([^,.;:!?^) \t-])" "“‘\\3" . -- "'word left - gsub "``" "“" . - gsub "''" "”" - escapeSingleQuotes = - gsub "'" "’" . -- otherwise right - gsub "'(&r[sd]quo;)" "’\\1" . -- never left quo before right quo - gsub "(&l[sd]quo;)'" "\\1‘" . -- never right quo after left quo - gsub "([ \t])'" "\\1‘" . -- never right quo after space - gsub "`" "‘" . -- ` is left - gsub "([^,.;:!?^) \t-])'" "\\1’" . -- word' right - gsub "^('|`)([^,.;:!?^) \t-])" "‘\\2" . -- 'word left - gsub "('|`)(\"|"|“|``)" "‘“" . -- '"word left - gsub "([^,.;:!?^) \t-])'(s|S)" "\\1’\\2" . -- possessive - gsub "([[:space:]])'([^,.;:!?^) \t-])" "\\1‘\\2" . -- 'word left - gsub "'([0-9][0-9](s|S))" "’\\1" -- '80s - decade abbrevs. - escapeDashes = gsub " ?-- ?" "—" . - gsub " ?--- ?" "—" . - gsub "([0-9])--?([0-9])" "\\1–\\2" - escapeEllipses = gsub "\\.\\.\\.|\\. \\. \\." "…" in - escapeSingleQuotes . escapeDoubleQuotes . escapeDashes . escapeEllipses . stringToHtml + let escapeDoubleQuotes = + gsub "(\"|")" "”" . -- rest are right quotes + gsub "(\"|")(&r[sd]quo;)" "”\\2" . + -- never left quo before right quo + gsub "(&l[sd]quo;)(\"|")" "\\2“" . + -- never right quo after left quo + gsub "([ \t])(\"|")" "\\1“" . + -- never right quo after space + gsub "(\"|")([^,.;:!?^) \t-])" "“\\2" . -- "word left + gsub "(\"|")('|`|‘)" "”’" . + -- right if it got through last filter + gsub "(\"|")('|`|‘)([^,.;:!?^) \t-])" "“‘\\3" . + -- "'word left + gsub "``" "“" . + gsub "''" "”" + escapeSingleQuotes = + gsub "'" "’" . -- otherwise right + gsub "'(&r[sd]quo;)" "’\\1" . -- never left quo before right quo + gsub "(&l[sd]quo;)'" "\\1‘" . -- never right quo after left quo + gsub "([ \t])'" "\\1‘" . -- never right quo after space + gsub "`" "‘" . -- ` is left + gsub "([^,.;:!?^) \t-])'" "\\1’" . -- word' right + gsub "^('|`)([^,.;:!?^) \t-])" "‘\\2" . -- 'word left + gsub "('|`)(\"|"|“|``)" "‘“" . -- '"word left + gsub "([^,.;:!?^) \t-])'(s|S)" "\\1’\\2" . -- possessive + gsub "([[:space:]])'([^,.;:!?^) \t-])" "\\1‘\\2" . -- 'word left + gsub "'([0-9][0-9](s|S))" "’\\1" -- '80s - decade abbrevs. + escapeDashes = + gsub " ?-- ?" "—" . + gsub " ?--- ?" "—" . + gsub "([0-9])--?([0-9])" "\\1–\\2" + escapeEllipses = gsub "\\.\\.\\.|\\. \\. \\." "…" in + escapeSingleQuotes . escapeDoubleQuotes . escapeDashes . + escapeEllipses . stringToHtml -- | Escape code string as needed for HTML. codeStringToHtml :: String -> String codeStringToHtml [] = [] codeStringToHtml (x:xs) = case x of - '&' -> "&" ++ codeStringToHtml xs - '<' -> "<" ++ codeStringToHtml xs - _ -> x:(codeStringToHtml xs) + '&' -> "&" ++ codeStringToHtml xs + '<' -> "<" ++ codeStringToHtml xs + _ -> x:(codeStringToHtml xs) -- | Escape string to HTML appropriate for attributes attributeStringToHtml :: String -> String @@ -109,17 +133,19 @@ attributeStringToHtml = gsub "\"" """ -- | Returns an HTML header with appropriate bibliographic information. htmlHeader :: WriterOptions -> Meta -> String htmlHeader options (Meta title authors date) = - let titletext = "" ++ (inlineListToHtml options title) ++ "\n" - authortext = if (null authors) then - "" - else - "\n" - datetext = if (date == "") then - "" - else - "\n" in - (writerHeader options) ++ authortext ++ datetext ++ titletext ++ "\n\n" + let titletext = "" ++ (inlineListToHtml options title) ++ + "\n" + authortext = if (null authors) + then "" + else "\n" + datetext = if (date == "") + then "" + else "\n" in + (writerHeader options) ++ authortext ++ datetext ++ titletext ++ + "\n\n" -- | Convert Pandoc block element to HTML. blockToHtml :: WriterOptions -> Block -> String @@ -128,85 +154,100 @@ blockToHtml options Null = "" blockToHtml options (Plain lst) = inlineListToHtml options lst blockToHtml options (Para lst) = "

" ++ (inlineListToHtml options lst) ++ "

\n" blockToHtml options (BlockQuote blocks) = - if (writerS5 options) then -- in S5, treat list in blockquote specially - -- if default is incremental, make it nonincremental; otherwise incremental - let inc = not (writerIncremental options) in - case blocks of - [BulletList lst] -> blockToHtml (options {writerIncremental = inc}) (BulletList lst) - [OrderedList lst] -> blockToHtml (options {writerIncremental = inc}) (OrderedList lst) - otherwise -> "
\n" ++ (concatMap (blockToHtml options) blocks) ++ - "
\n" - else - "
\n" ++ (concatMap (blockToHtml options) blocks) ++ "
\n" + if (writerS5 options) + then -- in S5, treat list in blockquote specially + -- if default is incremental, make it nonincremental; + -- otherwise incremental + let inc = not (writerIncremental options) in + case blocks of + [BulletList lst] -> blockToHtml (options {writerIncremental = + inc}) (BulletList lst) + [OrderedList lst] -> blockToHtml (options {writerIncremental = + inc}) (OrderedList lst) + otherwise -> "
\n" ++ + (concatMap (blockToHtml options) blocks) ++ + "
\n" + else "
\n" ++ (concatMap (blockToHtml options) blocks) ++ + "
\n" blockToHtml options (Note ref lst) = - let contents = (concatMap (blockToHtml options) lst) in - "
  • " ++ contents ++ "
  • \n" + let contents = (concatMap (blockToHtml options) lst) in + "
  • " ++ contents ++ "
  • \n" blockToHtml options (Key _ _) = "" -blockToHtml options (CodeBlock str) = "
    " ++ (codeStringToHtml str) ++ 
    -                                      "\n
    \n" +blockToHtml options (CodeBlock str) = + "
    " ++ (codeStringToHtml str) ++ "\n
    \n" blockToHtml options (RawHtml str) = str blockToHtml options (BulletList lst) = - let attribs = if (writerIncremental options) then " class=\"incremental\"" else "" in - "\n" ++ (concatMap (listItemToHtml options) lst) ++ "\n" + let attribs = if (writerIncremental options) + then " class=\"incremental\"" + else "" in + "\n" ++ (concatMap (listItemToHtml options) lst) ++ + "\n" blockToHtml options (OrderedList lst) = - let attribs = if (writerIncremental options) then " class=\"incremental\"" else "" in - "\n" ++ (concatMap (listItemToHtml options) lst) ++ "\n" + let attribs = if (writerIncremental options) + then " class=\"incremental\"" + else "" in + "\n" ++ (concatMap (listItemToHtml options) lst) ++ + "\n" blockToHtml options HorizontalRule = "
    \n" blockToHtml options (Header level lst) = - let contents = inlineListToHtml options lst in - let simplify = gsub "<[^>]*>" "" . gsub " " "_" in - if ((level > 0) && (level <= 6)) - then "\n" ++ - "" ++ contents ++ - "\n" - else "

    " ++ contents ++ "

    \n" -listItemToHtml options list = "
  • " ++ (concatMap (blockToHtml options) list) ++ "
  • \n" + let contents = inlineListToHtml options lst in + let simplify = gsub "<[^>]*>" "" . gsub " " "_" in + if ((level > 0) && (level <= 6)) + then "\n" ++ + "" ++ contents ++ + "\n" + else "

    " ++ contents ++ "

    \n" +listItemToHtml options list = + "
  • " ++ (concatMap (blockToHtml options) list) ++ "
  • \n" -- | Convert list of Pandoc inline elements to HTML. inlineListToHtml :: WriterOptions -> [Inline] -> String inlineListToHtml options lst = - -- consolidate adjacent Str and Space elements for more intelligent - -- smart typography filtering - let lst' = consolidateList lst in - concatMap (inlineToHtml options) lst' + -- consolidate adjacent Str and Space elements for more intelligent + -- smart typography filtering + let lst' = consolidateList lst in + concatMap (inlineToHtml options) lst' -- | Convert Pandoc inline element to HTML. inlineToHtml :: WriterOptions -> Inline -> String -inlineToHtml options (Emph lst) = "" ++ (inlineListToHtml options lst) ++ "" -inlineToHtml options (Strong lst) = "" ++ (inlineListToHtml options lst) ++ "" -inlineToHtml options (Code str) = "" ++ (codeStringToHtml str) ++ "" -inlineToHtml options (Str str) = if (writerSmart options) then - stringToSmartHtml str - else - stringToHtml str +inlineToHtml options (Emph lst) = + "" ++ (inlineListToHtml options lst) ++ "" +inlineToHtml options (Strong lst) = + "" ++ (inlineListToHtml options lst) ++ "" +inlineToHtml options (Code str) = + "" ++ (codeStringToHtml str) ++ "" +inlineToHtml options (Str str) = + if (writerSmart options) then stringToSmartHtml str else stringToHtml str inlineToHtml options (TeX str) = (codeStringToHtml str) inlineToHtml options (HtmlInline str) = str inlineToHtml options (LineBreak) = "
    \n" inlineToHtml options Space = " " inlineToHtml options (Link text (Src src tit)) = - let title = attributeStringToHtml tit in - if (isPrefixOf "mailto:" src) then - obfuscateLink options text src - else - "" else ">") ++ - (inlineListToHtml options text) ++ "" -inlineToHtml options (Link text (Ref [])) = "[" ++ (inlineListToHtml options text) ++ "]" -inlineToHtml options (Link text (Ref ref)) = "[" ++ (inlineListToHtml options text) ++ "][" ++ - (inlineListToHtml options ref) ++ "]" -- this is what markdown does, for better or worse + let title = attributeStringToHtml tit in + if (isPrefixOf "mailto:" src) + then obfuscateLink options text src + else "" else ">") ++ + (inlineListToHtml options text) ++ "" +inlineToHtml options (Link text (Ref [])) = + "[" ++ (inlineListToHtml options text) ++ "]" +inlineToHtml options (Link text (Ref ref)) = + "[" ++ (inlineListToHtml options text) ++ "][" ++ + (inlineListToHtml options ref) ++ "]" + -- this is what markdown does, for better or worse inlineToHtml options (Image alt (Src source tit)) = - let title = attributeStringToHtml tit - alternate = inlineListToHtml options alt in - "\""" + let title = attributeStringToHtml tit + alternate = inlineListToHtml options alt in + "\""" inlineToHtml options (Image alternate (Ref [])) = - "![" ++ (inlineListToHtml options alternate) ++ "]" + "![" ++ (inlineListToHtml options alternate) ++ "]" inlineToHtml options (Image alternate (Ref ref)) = - "![" ++ (inlineListToHtml options alternate) ++ "][" ++ (inlineListToHtml options ref) ++ "]" + "![" ++ (inlineListToHtml options alternate) ++ "][" ++ + (inlineListToHtml options ref) ++ "]" inlineToHtml options (NoteRef ref) = - "" ++ ref ++ "" - + "" ++ ref ++ "" -- cgit v1.2.3