~alcinnz/rhapsode

8f54b7a0753d73243e92fe9e186116ebeb8e8fc5 — Adrian Cochrane 3 years ago 911f852
Allow linking to any element on a webpage, regardless of whether it has an ID
1 files changed, 14 insertions(+), 0 deletions(-)

M src/Render.hs
M src/Render.hs => src/Render.hs +14 -0
@@ 11,6 11,7 @@ import System.Directory as Dir
import Data.FileEmbed

import Data.Maybe (fromMaybe, maybeToList)
import Text.Read (readMaybe)
import Debug.Trace (trace)

--- External Rhapsode subcomponents


@@ 108,6 109,19 @@ loadUserStyles styles = do

targetSel "" = [CSSTok.Ident "main"]
targetSel "#" = [CSSTok.Colon, CSSTok.Ident "root"]
targetSel ('#':'.':anchor) =
    CSSTok.Colon : CSSTok.Ident "root" : concat [ selLayer n | n <- path anchor]
  where
    path ('.':anchor) = [] : path anchor
    path (c:anchor) | n:path' <- path anchor, c >= '0' && c <= '9' = (c:n):path'
    path [] = [[]]
    path _ = []

    selLayer n = [
        CSSTok.Delim '>',
        CSSTok.Colon, CSSTok.Function "nth-child",
        CSSTok.Number (pack n) (CSSTok.NVInteger $ fromMaybe 0 $ readMaybe n),
        CSSTok.RightParen]
targetSel ('#':id) = [CSSTok.Hash CSSTok.HUnrestricted $ Txt.pack id]
targetSel _ = []