summaryrefslogtreecommitdiff
path: root/hs/src/Handlingar/Common.hs
blob: 16b5591c5be14f06ffca2eef23926b7c24c14f14 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
{-# LANGUAGE OverloadedStrings
           , ImportQualifiedPost
           #-}

module Handlingar.Common
( buildPrimary
, decoder
, findAlternative
, formatMail
, getVimwikiPage
, handleBilaga
, handleBilagaHeading
, handleBlocks
, handleMailLink
, replaceLinks
, shorten
, uriChars
, HandlingarOps(..)
) where

import Control.Monad.State.Lazy
import Data.ByteString (ByteString)
import Data.Default (def)
import Data.Map ((!))
import Data.Maybe (fromMaybe)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text, pack, unpack, strip)
import Data.Text.Encoding (decodeLatin1, decodeUtf8)
import Data.Text.IO qualified as T
import Mail (getMail, MailPart(..), getBytes, getFile)
import Network.URI (URI, uriPath)
import Network.URI.Encode (decode)
import System.FilePath (takeExtension, (<.>))
import System.IO (Handle)
import System.Process (cleanupProcess)
import Text.Pandoc (readVimwiki, readHtml, PandocMonad, PandocIO, writePlain)
import Text.Pandoc.Builder
import Text.Pandoc.Extract (AppendixItem, extractKV, getHeadingData)
import Text.Pandoc.Items (comment, dlist)
import Text.Pandoc.Walk (walk, walkM)
import Util (splitBy, (<&>))
import Network.URI (parseURI, uriScheme)
import Network.URI.Encode (encodeWith)
import Text.URI.Decode (urlDecode)


data HandlingarOps = HandlingarOps
    -- For any file included from the base document as an appendix,
    -- which originally had a "file:" or "local:" scheme, generate
    -- appropriate resources for that element, and return what should
    -- be included in the appendix section.
    --
    { handleFile :: FilePath -- ^ Path to include, may be relative the current working directory.
                 -> String   -- ^ File extension of the given path, with a leading period
                 -> PandocIO (Maybe [Block])
    -- |
    , rewriteLink :: Inline -> State [AppendixItem] Inline
    , destination :: FilePath
    }

-- See also: Html rebuildLinks
handleBilaga :: HandlingarOps -> [Block] -> Text -> PandocIO (Maybe [Block])
handleBilaga ops currentPage url = case parseURI . encodeWith (`elem` uriChars) . unpack $ url of
    Just uri -> case uriScheme uri of
                    -- A link to another wiki.
                    -- Currently implemented by simply mentioning
                    -- where to look
                    ('w':'n':'.':wikiname) -> return . Just $ [Para [Str $ "Interwiki" <> pack wikiname]]
                    -- File, may (and hopefully is) within wiki the source tree
                    -- Note that both "file:" and "local:" gets turned
                    -- into "file:" by the pandoc parser.
                    "file:" -> do
                        let (Just fname) = urlDecode $ uriPath uri
                        (handleFile ops) fname $ takeExtension fname
                    -- An included email.
                    "mail:" -> Just <$> handleMailLink uri
                    _ -> return $ Just [Para [Str . pack . show $ uri]]
    -- This is an internal wiki link
    Nothing  -> case break (== '#') (unpack url) of
        ("", '#':frag)   -> return $ getHeadingData (pack frag) currentPage
        (page, '#':frag) -> getVimwikiPage (page <.> "wiki") <&> getHeadingData (pack frag)
        (page, "")       -> Just <$> getVimwikiPage (page <.> "wiki")
        _                -> return $ Just [Para [Str "ERROR"]]



-- | Generate a header tag from an appendix reference
handleBilagaHeading :: [Inline] -> Text -> Block
handleBilagaHeading is ref = Header 1 ("bilaga:" <> ref, [], []) is

-- | Extract header, metadata, and contents from a list of Pandoc Blocks.
-- Returns a three-tuple consisting of 
-- * the sections title,
-- * metadata from the section
-- * Further contents of the section
--
-- The title is gotten by rendering the first block in the list into a
-- plain string.
--
-- If the second argument is a definition list, then each of those
-- items will be extracted into a list of key, value pairs
-- 
handleBlocks :: PandocMonad m => [Block] -> m (Text, [(Text, Text)], [Block])
handleBlocks (head:DefinitionList definitions:blocks) = do
    heading <- writePlain def (Pandoc nullMeta [head])
    kvs <- mapM extractKV definitions
    return (strip heading, kvs, blocks)
handleBlocks (head:blocks) = do
    heading <- writePlain def (Pandoc nullMeta [head])
    return (strip heading, [], blocks)
handleBlocks blocks = return ("Heading missing", [], blocks)


shorten :: Block -> Block
shorten (BlockQuote _) = BlockQuote [ Para [ Code ("", [], []) "[...]" ] ]
shorten x = x




-- | Replace all links in document from source to destination form.
-- The source links are either regular absolute links, or relative
-- links to other items within the source tree. This function
-- translates one such link to a link which is on a suitable form form
-- the output.
-- * For TeX this will probably be a '\ref' element
-- * For HTML a link to another page.
--
-- Note that something which actually creates the output referenced
-- items is also needed.
replaceLinks :: (Inline -> State [AppendixItem] Inline) 
             -> Pandoc -> (Pandoc, [AppendixItem])
replaceLinks rewriteLink = flip runState [] . walkM rewriteLink


uriChars :: Set Char
uriChars = Set.fromList $ ":/?#"  -- Allows us to modifiy existing URL
                        <> "-._~" -- URL safe
                        <> ['A'..'Z'] <> ['a'..'z'] <> ['0'..'9']

getVimwikiPage :: FilePath -> PandocIO [Block]
getVimwikiPage path = do
    text <- liftIO $ T.readFile path
    Pandoc _ blocks <- readVimwiki def text
    return blocks

findAlternative :: [MailPart] -> Maybe MailPart
findAlternative [] = Nothing
findAlternative (m@MailPart { contentType = contentType }:xs) = case contentType of
    "text/html"  -> Just m
    "text/plain" -> Just m
    _            -> findAlternative xs


-- TODO where are these strings defined
-- Strict Bytestring
decoder :: String -> (ByteString -> Text)
decoder "iso-8859-1" = decodeLatin1
decoder "utf-8"      = decodeUtf8
-- decoder _            = decodeUtf8Lenient
decoder _            = decodeLatin1


handleMailBody :: (Handle, Handle) -> MailPart -> PandocIO [Block]
handleMailBody ports mail =
    case splitBy '/' $ contentType mail of
        ("multipart", "alternative") -> do
            case findAlternative $ reverse $ parts mail of
                Just part -> handleMailBody ports part
                Nothing   -> return [ Para [ Str "Couldn't find any suitable alternative" ] ]
        -- mixed, but any unknown should be treated as mixed
        ("multipart", _) -> concat <$> mapM (handleMailBody ports) (parts mail)
        ("text", "plain") -> do
            bytes <- liftIO $ getBytes (partId mail) ports
            let content = decoder (fromMaybe "ASCII" $ charset mail) bytes
            return [ CodeBlock ("", [], []) content ]
        ("text", "html") -> do
            bytes <- liftIO $ getBytes (partId mail) ports
            let content = decoder (fromMaybe "ASCII" $ charset mail) bytes
            pdoc <- readHtml def content
            -- TODO renumber links
            let Pandoc _ blocks = walk shorten pdoc
            return blocks
        ("image", _) -> do
            tmpFile <- liftIO $ getFile (partId mail) ports
            let img = [ Plain
                        [ Image ("", [], [])
                          [Str "Image Caption?"]
                          (pack tmpFile, "") ]]
            let figure = [ Figure ("", [], []) -- TODO figure ref
                      -- (Caption Nothing [Plain [Str . filename mail]])
                      (Caption Nothing [Plain [Str . pack . show $ filename mail]])
                      img
                      ]
            return img
        -- TODO
        -- ("application", "pdf") -> do
        _  -> return [ Header 2 ("", [], []) [ Str "Attachment omitted" ]
                     , dlist [ ("Content-Type", contentType mail)
                             , ("Filename", show . filename $ mail) ]
                     ]

-- TODO Titlecase the headers
-- TODO from and to should monospace the
--      address (but not the name)
formatMail :: MailPart -> (Handle, Handle) -> PandocIO [Block]
formatMail mail ports = do
    let keys = ["from", "to", "subject", "date"]
    let f key = ( [Str $ key <> ":"]
                , [[Plain [ Str . pack $ headers mail ! unpack key ]]]
                )
    let kvs = map f keys

    body <- handleMailBody ports mail

    return $ DefinitionList kvs : body


handleMailLink :: URI -> PandocIO [Block]
handleMailLink uri = do
    let id = decode . uriPath $ uri
    -- liftIO $ print id
    mail' <- liftIO $ getMail id
    -- TODO #short
    body <- case mail' of
        Left err -> return [ Para [ Str "From "
                                    , Code ("", [], []) "getMail:"  ]
                             , Para [Str . pack $ err]
                             , Para [Code ("", [], []) (pack id) ]]
        Right (mail, proc@(Just stdin, Just stdout, _, _)) -> do
            -- TODO "short" `in` uriFrag
            bs <- formatMail mail (stdout, stdin)
            liftIO $ cleanupProcess proc
            return bs
        -- TODO error
        _ -> return []

    return $ comment ("msg id: " <> pack id) <> body


buildPrimary :: PandocMonad m
             => HandlingarOps
             -> Pandoc
             -> m (Pandoc, [AppendixItem])
buildPrimary ops (Pandoc meta blocks) = do
    -- Pandoc meta blocks <- reader def text
    -- let Just blocks = getHeadingData heading blocks
    (heading, kvs, wantedBlocks') <- handleBlocks blocks
    let (pandoc, appendices) = replaceLinks (rewriteLink ops) $ Pandoc meta wantedBlocks'

    let pandoc' = foldl (flip . uncurry $ setMeta) pandoc
                $ ("title", heading) : kvs
    return (pandoc', appendices)