Verified Commit 5d1e2f4e authored by doshitan's avatar doshitan

Basic, but better nix syntax highlighting

Previously nix code would used bash/shell highlighting (the bash
definition lists `*.nix` as a supported extension), which is wrong and
looks broken.

This new definition is minimal, but at least differentiates comments,
strings, and some built-ins. It can grow over time. Eventually hope to
make it back up stream unless some else beats me to it.
parent be3d0148
<?xml version="1.0" encoding="UTF-8"?>
<language name="Nix" version="1" extensions="*.nix;" mimetype="application/x-nix">
<!-- (c) 2020 by Tanner Doshier (hi@doshitan.com)
Released under the LGPL -->
<highlighting>
<list name="keywords">
<item>if</item>
<item>then</item>
<item>else</item>
<item>with</item>
<item>let</item>
<item>in</item>
<item>rec</item>
<item>inherit</item>
<item>or</item>
</list>
<list name="builtins">
<item>builtins</item>
<item>baseNameOf</item>
<item>derivation</item>
<item>dirOf</item>
<item>true</item>
<item>false</item>
<item>null</item>
<item>isNull</item>
<item>toString</item>
<item>fetchTarball</item>
<item>import</item>
<item>map</item>
<item>removeAttrs</item>
</list>
<contexts>
<context name="base" attribute="Normal Text" lineEndContext="#stay">
<DetectChar char="#" attribute="Comment" context="comment" />
<keyword attribute="Keyword" context="#stay" String="keywords" />
<keyword attribute="Builtin" context="#stay" String="builtins" />
<IncludeRules context="strings" />
</context>
<context name="comment" attribute="Comment" lineEndContext="#pop">
</context>
<context name="strings" attribute="Normal Text" lineEndContext="#stay">
<DetectChar char="&quot;" context="simple_string" attribute="String" />
<DetectChar char="'" char1="'" context="string" attribute="String" />
</context>
<context name="simple_string" attribute="String" lineEndContext="#stay">
<IncludeRules context="interpolation" />
<DetectChar char="&quot;" context="#pop" attribute="String" />
</context>
<context name="string" attribute="String" lineEndContext="#stay">
<IncludeRules context="interpolation" />
<Detect2Chars char="'" char1="'" context="#pop" attribute="String" />
</context>
<context name="interpolation" attribute="Normal Text" lineEndContext="#stay">
<Detect2Chars char="$" char1="{" context="interpolation_val" attribute="Variable" />
</context>
<context name="interpolation_val" attribute="Normal Text" lineEndContext="#stay">
<DetectChar char="}" context="#pop" attribute="Variable" />
</context>
</contexts>
<itemDatas>
<itemData name="Normal Text" defStyleNum="dsNormal" />
<itemData name="Comment" defStyleNum="dsComment" />
<itemData name="Keyword" defStyleNum="dsKeyword" />
<itemData name="Builtin" defStyleNum="dsBuiltIn" />
<itemData name="String" defStyleNum="dsString" />
<itemData name="Variable" defStyleNum="dsVariable" />
</itemDatas>
</highlighting>
<general>
<comments>
<comment name="singleLine" start="#"/>
</comments>
<!-- <keywords casesensitive="1" weakDeliminator="^%#[]$._{}:-/" additionalDeliminator="`"/> -->
<keywords casesensitive="1" />
</general>
</language>
......@@ -35,6 +35,7 @@ executable site
, pandoc-sidenote
, pandoc-types
, process
, skylighting
, split
, rio
, temporary
......
......@@ -13,6 +13,7 @@ import qualified RIO.Map as M
import qualified RIO.Text as T
import Codec.Binary.UTF8.Generic (toString)
import Skylighting (loadSyntaxesFromDir)
import System.Environment (getEnv)
import System.IO.Error (isDoesNotExistError)
import System.Process (callCommand, readProcess)
......@@ -37,6 +38,10 @@ allContentPattern = ("posts/*" .||. "pages/**" .||. "projects/*") .&&. hasNoVers
main :: IO ()
main = do
hakyllConf <- getHakyllConf
customSyntaxMap <- either error return =<< loadSyntaxesFromDir ((providerDirectory hakyllConf) ++ "/assets/syntax")
let contentPandocCompiler = mPandocCompiler (Just customSyntaxMap)
hakyllWith hakyllConf $ do
tags <- buildTags ("posts/*" .&&. hasNoVersion .&&. complement "**.metadata") (fromCapture "tags/*/index.html" . safeName)
let postTagsCtx = postCtx tags
......@@ -143,13 +148,13 @@ main = do
match ("pages/**" .&&. complement (fromList specialPages) .&&. complement "**.metadata") $ do
route contentRoute
compile $ mPandocCompiler
compile $ contentPandocCompiler
>>= loadAndApplyTemplate "templates/post-or-page.html" pageCtx
>>= loadAndApplyTemplate "templates/default.html" baseCtx
match ("posts/*" .&&. hasNoVersion .&&. complement "**.metadata") $ do
route contentRoute
compile $ mPandocCompiler
compile $ contentPandocCompiler
>>= loadAndApplyTemplate "templates/post-or-page.html" postTagsCtx
>>= loadAndApplyTemplate "templates/default.html" postTagsCtx
......@@ -178,7 +183,7 @@ main = do
, pageCtx
]
mPandocCompiler
contentPandocCompiler
>>= loadAndApplyTemplate "templates/project.html" projectCtx
>>= loadAndApplyTemplate "templates/default.html" baseCtx
......
......@@ -3,26 +3,29 @@ module Site.Pandoc where
import Hakyll
import RIO
import qualified RIO.Text as T
import Skylighting (SyntaxMap, defaultSyntaxMap)
import Text.Pandoc (Block (Header))
import qualified Text.Pandoc as Pandoc
import Text.Pandoc.Options (HTMLMathMethod (PlainMath),
WriterOptions, writerHTMLMathMethod,
writerNumberSections, writerSectionDivs,
writerTOCDepth, writerTableOfContents,
writerTemplate)
writerSyntaxMap, writerTOCDepth,
writerTableOfContents, writerTemplate)
import Text.Pandoc.SideNote (usingSideNotes)
import Text.Pandoc.Walk (walk)
writerOptions :: WriterOptions
writerOptions = defaultHakyllWriterOptions { writerSectionDivs = True
, writerTableOfContents = True
}
writerOptions :: Maybe SyntaxMap -> WriterOptions
writerOptions mCustomSyntaxMap = defaultHakyllWriterOptions
{ writerSectionDivs = True
, writerTableOfContents = True
, writerSyntaxMap = maybe defaultSyntaxMap (<> defaultSyntaxMap) mCustomSyntaxMap
}
mPandocCompiler :: Compiler (Item String)
mPandocCompiler =
mPandocCompiler :: Maybe SyntaxMap -> Compiler (Item String)
mPandocCompiler mCustomSyntaxMap =
pandocCompilerWithTransform
defaultHakyllReaderOptions
writerOptions
(writerOptions mCustomSyntaxMap)
(usingSideNotes . walk bumpHeaders)
feedPandocCompiler :: Compiler (Item String)
......@@ -32,10 +35,11 @@ feedPandocCompiler = pandocCompilerWith defaultHakyllReaderOptions writerOptions
tocPandocCompiler :: Compiler (Item String)
tocPandocCompiler = pandocCompilerWith defaultHakyllReaderOptions writerOptions'
where writerOptions' = writerOptions { writerNumberSections = True
, writerTemplate = Just "$toc$"
, writerTOCDepth = 2
}
where writerOptions' = (writerOptions Nothing)
{ writerNumberSections = True
, writerTemplate = Just "$toc$"
, writerTOCDepth = 2
}
plainPandocCompiler :: Compiler (Item String)
plainPandocCompiler = getResourceBody >>= readPandoc >>= writePlain
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment