summaryrefslogtreecommitdiff
path: root/Pancake.hs
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2017-10-26 19:31:50 +0300
committerdefanor <defanor@uberspace.net>2017-10-26 19:31:50 +0300
commit178e8328df38d67cef388eabe4de19469aadc1e8 (patch)
treecff9e96a5ef446f94c9027af4344f0d4df8506e7 /Pancake.hs
parente9d5306f9df690803399f81bcc31d86e28084ab9 (diff)
Adjust file type detection
Mostly for Gopher.
Diffstat (limited to 'Pancake.hs')
-rw-r--r--Pancake.hs37
1 files changed, 24 insertions, 13 deletions
diff --git a/Pancake.hs b/Pancake.hs
index 0c9f8c5..2663f03 100644
--- a/Pancake.hs
+++ b/Pancake.hs
@@ -41,6 +41,7 @@ import qualified Data.ByteString.UTF8 as BSUTF8
import Control.Exception
import Text.Pandoc.Readers.Plain
import Text.Pandoc.Readers.Gopher
+import Control.Applicative
-- * Document reading
@@ -102,20 +103,22 @@ readDoc cmd uri = do
let reader = case (uriScheme uri, takeExtension $ uriPath uri) of
-- some exceptions and special cases (might be better to make
-- this configurable)
- ("http:", ".php") -> P.getReader "html"
- ("https:", ".php") -> P.getReader "html"
- ("http:", "") -> P.getReader "html"
- ("https:", "") -> P.getReader "html"
- ("gopher:", "") -> pure . P.StringReader . const $ pure . readGopher
- (_, "") -> pure . P.StringReader . const $ pure . readPlain
- (_, ".txt") -> pure . P.StringReader . const $ pure . readPlain
- (_, ".md") -> P.getReader "markdown"
+ ("http:", ".php") -> html
+ ("https:", ".php") -> html
+ ("http:", "") -> html
+ ("https:", "") -> html
("gopher:", ext) -> case splitDirectories $ uriPath uri of
- ("/":"0":_) -> pure . P.StringReader . const $ pure . readPlain
- ("/":"1":_) -> pure . P.StringReader . const $ pure . readGopher
- ("/":"h":_) -> P.getReader "html"
- _ -> P.getReader $ tail ext
- (_, ext) -> P.getReader $ tail ext
+ ("/":"1":_) -> gopher
+ ("/":"h":_) -> html
+ -- "0" should indicate plain text, but it's also the most
+ -- suitable option for non-html markup. Not sure about this
+ -- approach, but it's similar to ignoring HTTP content-type,
+ -- and will do for now: better to render documents nicely
+ -- when possible.
+ ("/":"0":_) -> byExtension ext <|> plain
+ -- unknown or unrecognized item type
+ _ -> byExtension ext <|> gopher
+ (_, ext) -> byExtension ext <|> plain
cols = maybe 80 id $ TI.getCapability term TI.termColumns
opts = def { P.readerColumns = cols }
case reader of
@@ -132,6 +135,14 @@ readDoc cmd uri = do
case r of
Left err -> putStrLn (show err) >> pure Nothing
Right (doc, _) -> pure $ pure doc
+ where
+ html = P.getReader "html"
+ plain = pure . P.StringReader . const $ pure . readPlain
+ gopher = pure . P.StringReader . const $ pure . readGopher
+ byExtension "" = Left "No extension"
+ byExtension ".md" = P.getReader "markdown"
+ byExtension ".txt" = pure . P.StringReader . const $ pure . readPlain
+ byExtension ext = P.getReader $ tail ext
-- * Rendering