Skip to content

Commit

Permalink
Implemented SCSS-like partial imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
h3rald committed Dec 9, 2018
1 parent 67c1494 commit 5d52d15
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 36 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

2 changes: 1 addition & 1 deletion config.nim
@@ -1,5 +1,5 @@
const
pkgName* = "HastySite"
pkgVersion* = "1.2.2"
pkgVersion* = "1.3.0"
pkgDescription* = "A small but powerful static site generator"
pkgAuthor* = "Fabio Cevasco"
24 changes: 23 additions & 1 deletion hastysite.nim
Expand Up @@ -78,6 +78,10 @@ let PEG_CSS_VAR_INSTANCE = peg"""
instance <- 'var(--' {id} ')'
id <- [a-zA-Z0-9_-]+
"""
let PEG_CSS_IMPORT = peg"""
import <- '@import' \s+ '\'' {partial} '\';'
partial <- [a-zA-Z0-9_-]+
"""

var CSS_VARS = initTable[string, string]()

Expand All @@ -100,6 +104,22 @@ proc processCssVariables(text: string): string =
else:
stderr.writeLine("CSS variable '$1' is not defined." % ["--" & id])

proc processCssImportPartials(text: string, hs: HastySite): string =
result = text
var folder = "assets/styles"
if hs.settings.hasKey("css-partials"):
folder = hs.settings["css-partials"].getStr
for def in result.findAll(PEG_CSS_IMPORT):
var matches: array[0..1, string]
discard def.match(PEG_CSS_IMPORT, matches)
let partial = folder/"_" & matches[0].strip & ".css"
var contents = ""
if partial.existsFile:
contents = partial.readFile
result = result.replace(def, contents)
else:
stderr.writeLine("@import: partial '$1' does not exist" % [partial])

proc preprocessContent(file, dir: string, obj: var JsonNode): string =
let fileid = file.replace(dir, "")
var f: File
Expand Down Expand Up @@ -243,6 +263,7 @@ proc init*(dir: string) =
json["temp"] = %"temp"
json["output"] = %"output"
json["scripts"] = %"scripts"
json["css-partials"] = %"assets/styles"
for key, value in json.pairs:
createDir(dir/value.getStr)
createDir(dir/"assets/fonts")
Expand Down Expand Up @@ -381,7 +402,8 @@ proc hastysite_module*(i: In, hs1: HastySite) =
def.symbol("preprocess-css") do (i: In):
var vals = i.expect("string")
let css = vals[0]
let res = css.getString.processCssVariables()
var res = css.getString.processCssImportPartials(hs)
res = res.processCssVariables()
i.push res.newVal()

def.symbol("mustache") do (i: In):
Expand Down
2 changes: 1 addition & 1 deletion site/contents/about.md
Expand Up @@ -15,7 +15,7 @@ Because HastySite:
* provides a simple but functional fully-working site template out-of-the-box, which is also the same template used for its [web site](https://hastysite.h3rald.com).
* provides out-of-the-box Markdown support. But not just any markdown, [HastyScribe](https://h3rald.com/hastyscribe)-compatible markdown, which extends the alredy-amazing and powerful [Discount](https://www.pell.portland.or.us/~orc/Code/discount/) engine with more useful features such as snippets, macros, fields and transclusion.
* provides a robust logic-less templating engine based on [mustache](https://mustache.github.io/).
* provides support for [CSS variables](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables), which doesn't substitute a full fledged CSS preprocessor like LESS or SASS, but it does help.
* provides support for SCSS-like partials and [CSS variables](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables), which don't substitute a full fledged CSS preprocessor like LESS or SASS, but they do help.


## Technology and Credits
Expand Down
2 changes: 1 addition & 1 deletion site/contents/getting-started.md
Expand Up @@ -4,7 +4,7 @@ title: "Getting Started"
content-type: page
-----

{{version => 1.2.2}}
{{version => 1.3.0}}

## Download

Expand Down
10 changes: 10 additions & 0 deletions site/contents/posts/v130-released.md
@@ -0,0 +1,10 @@
-----
id: v130-released
title: "Version 1.3.0 released"
content-type: post
date: "9 December 2018"
timestamp: 1544370271
-----

* Implemented basic support for SCSS-like partial imports.

33 changes: 22 additions & 11 deletions site/contents/reference.md
Expand Up @@ -131,21 +131,32 @@ Starts the preprocessing phase of the build.
#}

{#op||preprocess-css||{{s1}}||{{s2}}||
Pre-process [CSS variable](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables) declarations and usages within {{s1}}, returning the resulting CSS code {{s2}}.
Pre-process CSS contents within {{s1}}, i.e.:

For example, the following CSS code:
1. Pseudo-SCSS partial imports. Partial CSS files must start with an underscore and be placed in the `css-partials` directory (set in your [settings.json](class:file), or [assets/styles](class:dir) if not specified).
2. [CSS variable](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables) declarations

```
:root {
--standard-gray: #cccccc;
}
Returns the resulting CSS code {{s2}}.

.note {
background-color: var(--standard-gray);
}
```
For example, the following CSS code in two different files:


\_vars.css:
: ```
:root {
--standard-gray: #cccccc;
}
```
style.css:
: ```
@import 'vars';
.note {
background-color: var(--standard-gray);
}
```
Will be converted to the following:
Will be converted into the following:
```
:root {
Expand Down

0 comments on commit 5d52d15

Please sign in to comment.