Skip to content

Commit

Permalink
Various fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Jan 7, 2025
1 parent a70cc0e commit 3e6ed8e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 85 deletions.
118 changes: 35 additions & 83 deletions skylighting-format-typst/src/Skylighting/Format/Typst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import qualified Data.Map as Map
import Data.Text (Text)
import qualified Data.Text as Text
import Skylighting.Types
import Text.Printf
import qualified Data.Map as M
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup
#endif
Expand Down Expand Up @@ -59,87 +59,39 @@ formatTypstBlock opts ls =
-- | Converts a 'Style' to a set of Typst macro definitions,
-- which should be placed in the document's preamble.
styleToTypst :: Style -> Text
styleToTypst f = "TODO"


-- #let Skylighting(body, number: false, start: 1) = body
-- #let EndLine() = raw("\n")
--
-- #let KeywordTok(s) = raw(s)
-- #let DataTypeTok(s) = raw(s)
-- #let DecValTok(s) = raw(s)
-- #let BaseNTok(s) = raw(s)
-- #let FloatTok(s) = raw(s)
-- #let ConstantTok(s) = raw(s)
-- #let CharTok(s) = raw(s)
-- #let SpecialCharTok(s) = raw(s)
-- #let StringTok(s) = raw(s)
-- #let VerbatimStringTok(s) = raw(s)
-- #let SpecialStringTok(s) = raw(s)
-- #let ImportTok(s) = raw(s)
-- #let CommentTok(s) = raw(s)
-- #let DocumentationTok(s) = raw(s)
-- #let AnnotationTok(s) = raw(s)
-- #let CommentVarTok(s) = raw(s)
-- #let OtherTok(s) = raw(s)
-- #let FunctionTok(s) = raw(s)
-- #let VariableTok(s) = raw(s)
-- #let ControlFlowTok(s) = raw(s)
-- #let OperatorTok(s) = raw(s)
-- #let BuiltInTok(s) = raw(s)
-- #let ExtensionTok(s) = raw(s)
-- #let PreprocessorTok(s) = raw(s)
-- #let AttributeTok(s) = raw(s)
-- #let RegionMarkerTok(s) = raw(s)
-- #let InformationTok(s) = raw(s)
-- #let WarningTok(s) = raw(s)
-- #let AlertTok(s) = raw(s)
-- #let ErrorTok(s) = raw(s)
-- #let NormalTok(s) = raw(s)


-- define Skylighting + all the token functions
-- (case backgroundColor f of
-- Nothing -> ["\\newenvironment{Shaded}{}{}"]
-- Just (RGB r g b) -> ["\\usepackage{framed}"
-- ,Text.pack
-- (printf "\\definecolor{shadecolor}{RGB}{%d,%d,%d}" r g b)
-- ,"\\newenvironment{Shaded}{\\begin{snugshade}}{\\end{snugshade}}"])
-- ++ sort (map (macrodef (defaultColor f) (Map.toList (tokenStyles f)))
-- (enumFromTo KeywordTok NormalTok))

-- also define EndLine
-- #raw(\"\\n\")"
styleToTypst f =
Text.unlines $
[ "#let Skylighting(body, number: false, start: 1) = block(" <>
(case backgroundColor f of
Nothing -> ""
Just c -> "fill: rgb(" <> Text.pack (show (fromColor c :: String)) <> "), ")
<> "body)"
, "#let EndLine() = raw(\"\\n\")"
] <>
sort (map (macrodef (defaultColor f) (Map.toList (tokenStyles f)))
(enumFromTo KeywordTok NormalTok))

macrodef :: Maybe Color -> [(TokenType, TokenStyle)] -> TokenType -> Text
macrodef defaultcol tokstyles tokt = "TODO"
-- "\\newcommand{\\"
-- <> Text.pack (show tokt)
-- <> "}[1]{"
-- <> Text.pack (co . ul . bf . it . bg $ "#1")
-- <> "}"
-- where tokf = case lookup tokt tokstyles of
-- Nothing -> defStyle
-- Just x -> x
-- ul x = if tokenUnderline tokf
-- then "\\underline{" <> x <> "}"
-- else x
-- it x = if tokenItalic tokf
-- then "\\textit{" <> x <> "}"
-- else x
-- bf x = if tokenBold tokf
-- then "\\textbf{" <> x <> "}"
-- else x
-- bcol = fromColor `fmap` tokenBackground tokf
-- :: Maybe (Double, Double, Double)
-- bg x = case bcol of
-- Nothing -> x
-- Just (r, g, b) ->
-- printf "\\colorbox[rgb]{%0.2f,%0.2f,%0.2f}{%s}" r g b x
-- col = fromColor `fmap` (tokenColor tokf `mplus` defaultcol)
-- :: Maybe (Double, Double, Double)
-- co x = case col of
-- Nothing -> x
-- Just (r, g, b) ->
-- printf "\\textcolor[rgb]{%0.2f,%0.2f,%0.2f}{%s}" r g b x

macrodef defaultcol tokstyles' tokt =
"#let " <> Text.pack (show tokt) <> "(s) = " <> (ul . bg . textstyle) ("raw(s)")
where tokstyles = M.fromList tokstyles'
tokf = case M.lookup tokt tokstyles of
Nothing -> defStyle
Just x -> x
ul x = if tokenUnderline tokf
then "underline(" <> x <> ")"
else x
bg x = case tokenBackground tokf of
Nothing -> x
Just _c -> x -- TODO?
textstyle x = "text(" <> bf x <> it x <> co x <> x <> ")"
it x = if tokenItalic tokf
then "style: \"italic\","
else ""
bf x = if tokenBold tokf
then "weight: \"bold\","
else ""
co x = case tokenColor tokf `mplus` defaultcol of
Just c -> "fill: rgb(" <>
Text.pack (show (fromColor c :: String)) <> "),"
Nothing -> ""
4 changes: 2 additions & 2 deletions skylighting/bin/main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ hlTypst :: Bool -- ^ Fragment
hlTypst frag fname opts sty sourceLines =
if frag
then Text.putStrLn fragment
else Text.putStrLn $ macros <> pageTitle <> fragment
else Text.putStrLn $ macros <> "\n" <> pageTitle <> "\n" <> fragment
where fragment = formatTypstBlock opts sourceLines
macros = styleToTypst sty
pageTitle = "== " <> Text.pack fname <> "\n"
pageTitle = "= " <> Text.pack fname <> "\n"


hlConTeXt :: Bool -- ^ Fragment
Expand Down

0 comments on commit 3e6ed8e

Please sign in to comment.