This repository has been archived by the owner on Aug 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markups.hs
68 lines (59 loc) · 3.94 KB
/
Markups.hs
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
{-# LANGUAGE PatternGuards, OverloadedStrings #-}
module Markups (addAmazonAssociateLink, readers, renderMarkup) where
import Text.Pandoc.Generic
import qualified Data.ByteString.Char8 as BS
import qualified Data.Text as T
import Network.HTTP.Types
import Data.List
import qualified Data.CaseInsensitive as CI
import Blaze.ByteString.Builder
import Text.Pandoc
import Yesod hiding (insert)
import Data.Maybe
import Prelude
import Data.Char
import Control.Monad
renderTwitterLink :: Pandoc -> Pandoc
renderTwitterLink = bottomUp go
where
go (Link is (url, title))
| ("twitter":xs) <- T.split (==':') $ T.pack url
= Link is (T.unpack $ T.append "http://twitter.com/#!/" $ T.intercalate ":" xs, title)
go i = i
addAmazonAssociateLink :: String -> Pandoc -> Pandoc
addAmazonAssociateLink = bottomUp . procAmazon
renderMarkup :: Monad m
=> Maybe String
-> String -- ^ markup language
-> (Pandoc -> m Pandoc) -- ^ pandoc transformer
-> String -- ^ source
-> m Html -- ^ Html
renderMarkup mid lang trans =
liftM (writeHtml opts)
. trans . renderTwitterLink
. fromMaybe readMarkdown (lookup (map toLower lang) readers) defaultParserState
where
opts = defaultWriterOptions
{ writerHTMLMathMethod =
MathJax "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
, writerHighlight = True
, writerHtml5 = True
, writerIdentifierPrefix = fromMaybe "" mid
}
procAmazon :: String -> Inline -> Inline
procAmazon tag (Link is (url, title)) = Link is (attachTo tag url, title)
procAmazon tag (Image is (url, title)) = Image is (attachTo tag url, title)
procAmazon _ il = il
amazons :: [T.Text]
amazons = "www.amazon.com":"amazon.com":concatMap (\cc -> [T.concat [www,"amazon.",co,cc] | www <- ["","www."], co <- ["co.", ""]]) ccTLDs
attachTo :: String -> String -> String
attachTo key url | (p@("http:":"":amazon:paths), qs) <- decodePath (BS.pack url)
, amazon `elem` amazons
, let cipath = map CI.mk paths
, ["o", "asin"] `isPrefixOf` cipath || "dp" `elem` cipath
|| ["gp", "product"] `isPrefixOf` cipath
, isNothing (lookup "tag" qs)
= tail $ BS.unpack $ toByteString $ encodePath p (("tag",Just $ BS.pack key):qs)
attachTo _ url = url
ccTLDs :: [T.Text]
ccTLDs = ["ac", "ad", "ae", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "as", "at", "au", "aw", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tm", "tn", "to", "tp", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "um", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "yu", "za", "zm", "zw"]