Skip to content

Commit

Permalink
Merge branch 'strip-header'
Browse files Browse the repository at this point in the history
  • Loading branch information
koterpillar committed Oct 28, 2015
2 parents 0b68470 + be1d9c0 commit 6ab8bb5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
28 changes: 28 additions & 0 deletions src/Models.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import Control.Monad
import Control.Monad.State

import qualified Data.Map as M
import Data.Maybe
import qualified Data.Set as S
import Data.Time

import Text.Pandoc hiding (Meta, readers)
import Text.Pandoc.Walk

import Language
import Utils
Expand Down Expand Up @@ -114,3 +116,29 @@ allLanguages app = S.union articleLangs metaLangs
allContentLangs = S.unions . map contentLangs
contentLangs :: HasContent a => a -> S.Set Language
contentLangs = S.fromList . M.keys . getContent

langTitle :: HasContent a => LanguagePreference -> a -> String
langTitle lang = fromMaybe "untitled" . listToMaybe . query extractTitle . langContent lang
where extractTitle (Header _ _ title) = [inlineToStr title]
extractTitle _ = []

-- TODO: Might be a better way to do this in Pandoc
inlineToStr :: [Inline] -> String
inlineToStr inline = writePlain def $ Pandoc undefined [Plain inline]

stripTitle :: Pandoc -> Pandoc
stripTitle (Pandoc meta blocks) = Pandoc meta blocks'
where blocks' = catMaybes $ evalState (mapM stripFirst blocks) True
stripFirst :: Block -> State Bool (Maybe Block)
stripFirst block = do
isFirst <- get
if isFirst
then case block of
Header _ _ _ -> do
put False
return Nothing
_ -> return $ Just block
else return $ Just block

langContent :: HasContent a => LanguagePreference -> a -> Pandoc
langContent lang = fromJust . matchLanguage lang . getContent
12 changes: 0 additions & 12 deletions src/Views.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,6 @@ metaDisplay :: (MonadRoute m, URL m ~ Sitemap, MonadState AppState m, MonadPlus
metaDisplay lang meta = template lang $
mkPage (Just $ langTitle lang meta) $(hamletFile "templates/meta.hamlet")

langTitle :: HasContent a => LanguagePreference -> a -> String
langTitle lang = fromMaybe "untitled" . listToMaybe . query extractTitle . langContent lang
where extractTitle (Header _ _ title) = [inlineToStr title]
extractTitle _ = []

-- TODO: Might be a better way to do this in Pandoc
inlineToStr :: [Inline] -> String
inlineToStr inline = writePlain def $ Pandoc undefined [Plain inline]

langContent :: HasContent a => LanguagePreference -> a -> Pandoc
langContent lang = fromJust . matchLanguage lang . getContent

-- Generate a link to some content
linkTo :: (Linkable a, MonadRoute m, URL m ~ Sitemap)
=> a
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Feed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ articleEntry lang article = do
articleLink <- linkTo article
authorName <- getLangString lpref "authorName"
let entry = nullEntry articleLink (TextString $ langTitle lpref article) (atomDate $ arAuthored article)
let content = renderMarkup $ writeHtml def $ langContent lpref article
let content = renderMarkup $ writeHtml def $ stripTitle $ langContent lpref article
return entry { entryContent = Just $ HTMLContent content
, entryLinks = [nullLink articleLink]
, entryAuthors = [nullPerson { personName = authorName }]
Expand Down
4 changes: 0 additions & 4 deletions testsuite/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ module Arbitrary where

import Prelude hiding (LT)

import Control.Applicative

import Data.DeriveTH
import Data.LanguageCodes
import qualified Data.Map as M
import Data.Time

import Text.Pandoc

import Language

import Test.Framework

instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (M.Map k v) where
Expand Down
3 changes: 3 additions & 0 deletions testsuite/Integration/TestRSS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ test_home = do
let entry1 = head entries
assertEqual ["Author Name"]
$ map personName $ entryAuthors entry1
let Just (HTMLContent content) = entryContent entry1
assertEqual "<p>This article should appear above the first one.</p>"
$ content

0 comments on commit 6ab8bb5

Please sign in to comment.