@@ 1,5 1,6 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE PatternSynonyms, ViewPatterns #-}
module Input(fetchDocument, docForText) where
import Data.Text.Lazy (fromStrict)
@@ 27,10 28,14 @@ import Data.Maybe (fromMaybe)
import Foreign.StablePtr
import Foreign.C.String
+utf8' bytes = convertCharset "utf-8" $ B.toStrict bytes
+
fetchDocument http mime uri = fetchURL' http mime uri >>= parseDocument http
parseDocument sess (uri, "html/x-error\t", resp) = parseDocument sess (uri, "text/html", resp)
parseDocument _ (_, "text/html", Left text) = return $ HTML.parseLT $ fromStrict text
parseDocument _ (_, "text/html", Right bytes) = return $ HTML.parseLBS bytes
+parseDocument _ (_, "text/gemini", Left text) = return $ parseGemini text
+parseDocument _ (_, "text/gemini", Right bytes) = return $ parseGemini $ utf8' bytes
parseDocument _ (_, _, Left text)
| Right doc <- XML.parseText def $ fromStrict text = return doc
@@ 38,7 43,7 @@ parseDocument _ (_, _, Left text)
parseDocument _ (_, _, Right bytes) | Right doc <- XML.parseLBS def bytes = return doc
parseDocument _ (_, 't':'e':'x':'t':'/':_, Right bytes) =
-- charset wasn't specified, so assume utf-8.
- return $ docForText $ convertCharset "utf-8" $ B.toStrict bytes
+ return $ docForText $ utf8' bytes
parseDocument sess resp@(_, mime, _) = do
dir <- getCurrentDirectory -- TODO find Downloads directory.
ret <- saveDownload nullURI {
@@ 58,6 63,58 @@ docForText txt = XML.Document {
}
--------
+---- Gemini implementation
+--------
+-- Copied from css-syntax.
+pattern (:.) :: Char -> Txt.Text -> Txt.Text
+pattern x :. xs <- (Txt.uncons -> Just (x, xs))
+
+infixr 5 :.
+
+el name text = XML.Element name M.empty [XML.NodeContent text]
+
+parseGemini :: Txt.Text -> XML.Document
+parseGemini txt = XML.Document {
+ XML.documentPrologue = XML.Prologue [] Nothing [],
+ XML.documentRoot = XML.Element {
+ XML.elementName = "body",
+ XML.elementAttributes = M.empty,
+ XML.elementNodes = map XML.NodeElement $ parseGemini' $ Txt.lines txt
+ },
+ XML.documentEpilogue = []
+ }
+
+parseGemini' :: [Txt.Text] -> [XML.Element]
+parseGemini' (('#':.'#':.'#' :. '#':.'#':.'#':.line):lines) =
+ el "h6" line : parseGemini' lines
+parseGemini' (('#':.'#':.'#' :. '#':.'#':.line):lines) =
+ el "h5" line : parseGemini' lines
+parseGemini' (('#':.'#':.'#' :. '#':.line):lines) =
+ el "h4" line : parseGemini' lines
+parseGemini' (('#':.'#':.'#':.line):lines) = el "h3" line : parseGemini' lines
+parseGemini' (('#':.'#':.line):lines) = el "h2" line : parseGemini' lines
+parseGemini' (('#':.line):lines) = el "h1" line : parseGemini' lines
+-- Not properly structured, but still sounds fine...
+parseGemini' (('*':.line):lines) = el "li" line : parseGemini' lines
+
+parseGemini' (('=':.'>':.line):lines)
+ | (url:text@(_:_)) <- Txt.words line = (el "a" $ Txt.unwords text) {
+ XML.elementAttributes = M.insert "href" url M.empty
+ } : parseGemini' lines
+ | otherwise = (el "a" $ Txt.strip line) {
+ XML.elementAttributes = M.insert "href" (Txt.strip line) M.empty
+ } : parseGemini' lines
+parseGemini' (('`':.'`':.'`':.line):lines) = go [line] lines
+ where
+ go texts ("```":lines) = el "pre" (Txt.unlines texts) : parseGemini' lines
+ go texts (('`':.'`':.'`':.line):lines) =
+ el "pre" (Txt.unlines texts) : el "p" line : parseGemini' lines
+ go texts (line:lines) = go (texts ++ [line]) lines
+ go texts [] = []
+parseGemini' (line:lines) = el "p" line : parseGemini' lines
+parseGemini' [] = []
+
+--------
---- C API
--------
foreign export ccall c_newSession :: IO (StablePtr Session)