Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring specific headers in theme #162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,22 @@ and `onRgb#101060` for a deep purple background). Naturally, your terminal
needs to support 24-bit RGB for this to work. When creating portable
presentations, it might be better to stick with the named colours listed above.

#### Theming headers

In addition to `header`, individual headers can also be customized. The
configuration blocks under `headers` accepts a `style` list, a `prefix` string
and an `underline` string that is repeated match the width of the header.

```yaml
patat:
theme:
headers:
h3:
style: [vividRed]
prefix: '### '
underline: '-~-~'
```

### Syntax Highlighting

`patat` uses [Kate] Syntax Highlighting files. `patat` ships with support for
Expand Down
23 changes: 18 additions & 5 deletions lib/Patat/Presentation/Display.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ module Patat.Presentation.Display


--------------------------------------------------------------------------------
import Control.Monad (guard)
import Control.Monad (guard, mplus)
import qualified Data.Aeson.Extended as A
import Data.Char.WCWidth.Extended (wcstrwidth)
import Data.Data.Extended (grecQ)
import qualified Data.List as L
import qualified Data.Map as M
import Data.Maybe (fromMaybe, maybeToList)
import qualified Data.Sequence.Extended as Seq
import qualified Data.Text as T
Expand All @@ -23,7 +24,7 @@ import Patat.Presentation.Display.CodeBlock
import Patat.Presentation.Display.Internal
import Patat.Presentation.Display.Table
import Patat.Presentation.Internal
import Patat.PrettyPrint ((<$$>), (<+>))
import Patat.PrettyPrint ((<$$>))
import qualified Patat.PrettyPrint as PP
import Patat.Size
import Patat.Theme (Theme (..))
Expand Down Expand Up @@ -206,9 +207,21 @@ prettyBlock ds (Pandoc.Plain inlines) = prettyInlines ds inlines
prettyBlock ds (Pandoc.Para inlines) =
prettyInlines ds inlines <> PP.hardline

prettyBlock ds (Pandoc.Header i _ inlines) =
themed ds themeHeader (PP.string (replicate i '#') <+> prettyInlines ds inlines) <>
PP.hardline
prettyBlock ds (Pandoc.Header n _ inlines) =
themed ds style content <> PP.hardline <>
(case underline of
Nothing -> mempty
Just t ->
themed ds style (PP.string $ take cols $ cycle $ T.unpack t) <>
PP.hardline)
where
prefix = fromMaybe mempty $ config (dsTheme ds) >>= Theme.htPrefix
content = PP.text prefix <> prettyInlines ds inlines
(_, cols) = PP.dimensions content
underline = config (dsTheme ds) >>= Theme.htUnderline

style t = (config t >>= Theme.htStyle) `mplus` themeHeader t
config t = themeHeaders t >>= M.lookup ("h" ++ show n)

prettyBlock ds (Pandoc.CodeBlock (_, classes, _) txt) =
prettyCodeBlock ds classes txt
Expand Down
20 changes: 18 additions & 2 deletions lib/Patat/Theme.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Patat.Theme
( Theme (..)
( HeaderTheme (..)
, Theme (..)
, defaultTheme

, Style (..)
Expand Down Expand Up @@ -31,10 +32,19 @@ import qualified System.Console.ANSI as Ansi
import Text.Read (readMaybe)


--------------------------------------------------------------------------------
data HeaderTheme = HeaderTheme
{ htStyle :: !(Maybe Style)
, htPrefix :: !(Maybe T.Text)
, htUnderline :: !(Maybe T.Text)
} deriving (Show)


--------------------------------------------------------------------------------
data Theme = Theme
{ themeBorders :: !(Maybe Style)
, themeHeader :: !(Maybe Style)
, themeHeaders :: !(Maybe (M.Map String HeaderTheme))
, themeCodeBlock :: !(Maybe Style)
, themeBulletList :: !(Maybe Style)
, themeBulletListMarkers :: !(Maybe T.Text)
Expand Down Expand Up @@ -65,6 +75,7 @@ instance Semigroup Theme where
l <> r = Theme
{ themeBorders = mplusOn themeBorders
, themeHeader = mplusOn themeHeader
, themeHeaders = mappendOn themeHeaders
, themeCodeBlock = mplusOn themeCodeBlock
, themeBulletList = mplusOn themeBulletList
, themeBulletListMarkers = mplusOn themeBulletListMarkers
Expand Down Expand Up @@ -99,13 +110,17 @@ instance Monoid Theme where
mempty = Theme
Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing Nothing

--------------------------------------------------------------------------------
defaultTheme :: Theme
defaultTheme = Theme
{ themeBorders = dull Ansi.Yellow
, themeHeader = dull Ansi.Blue
, themeHeaders = Just $ M.fromList $ do
n <- [1 .. 6]
let prefix = T.replicate n "#" <> " "
pure ("h" <> show n, HeaderTheme Nothing (Just prefix) Nothing)
, themeCodeBlock = dull Ansi.White `mappend` ondull Ansi.Black
, themeBulletList = dull Ansi.Magenta
, themeBulletListMarkers = Just "-*"
Expand Down Expand Up @@ -322,4 +337,5 @@ syntaxHighlight theme tokenType = do


--------------------------------------------------------------------------------
$(A.deriveJSON A.dropPrefixOptions ''HeaderTheme)
$(A.deriveJSON A.dropPrefixOptions ''Theme)
23 changes: 23 additions & 0 deletions tests/golden/inputs/theme-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
patat:
theme:
headers:
h1:
underline: "=~"
h3:
style: [vividRed]
prefix: '== '
underline: '^ '
...

# Header 1

## Header 2

### Header 3

Some Content

### A long Header 3

More Content
48 changes: 48 additions & 0 deletions tests/golden/outputs/theme-headers.md.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
 theme-headers.md 








 Header 1
 =~=~=~=~

 1 / 4 

{slide}
 theme-headers.md > Header 1 









 ## Header 2

 2 / 4 

{slide}
 theme-headers.md > Header 1 > Header 2 

== Header 3
^ ^ ^ ^ ^ ^

Some Content

 3 / 4 

{slide}
 theme-headers.md > Header 1 > Header 2 

== A long Header 3
^ ^ ^ ^ ^ ^ ^ ^ ^ 

More Content

 4 / 4 
Loading