I’m currently toying around with processing a Jekyll site with YS as a way to finally write some real YS code. Is there a way to quote a text file (e.g. an Liquid template file) as a YAML string from YAML or YS (ideally without having to modify the file)? I’m trying to avoid YS processing template literals as YAML. Most of the content files can be loaded without incident, but the layout files blow up.
$ cat jekyll.yaml
from:
file: |
{% if page.show_sidebar %}
<div class="sidebar">
sidebar content
</div>
{% endif %}
$ ys -l jekyll.yaml -e '.file:print'
{% if page.show_sidebar %}
<div class="sidebar">
sidebar content
</div>
{% endif %}Quoting files as strings (like heredocs in come langs) is one of the things YAML (and thus YS) is great at. Just use | and then indent the file content appropriately.
In YAML, this is called a "literal" style scalar. There are 5 scalar styles: plain (unquoted), single quoted, double quoted, literal and folded.
Note, that in YS you can interpolate variables an d expressions in double quoted and literal strings. This makes literal strings excellent you simple templating:
$ cat interpolate.ys
!yamlscript/v0
say: "Hello $(ENV.USER:uc1). 2 + 2 = $(2 + 2)."
say: |
Have you seen this cool new program?
$(FILE:slurp)
It's written in YAMLScript v$VERSION
$ ys interpolate.ys
Hello Ingy. 2 + 2 = 4.
Have you seen this cool new program?
!yamlscript/v0
say: "Hello $(ENV.USER:uc1). 2 + 2 = $(2 + 2)."
say: |
Have you seen this cool new program?
$(FILE:slurp)
It's written in YAMLScript v$VERSION
It's written in YAMLScript v0.1.87
$ https://gist.github.com/ingydotnet/9ece4af186c6a6dcfd589c446dab9b38 was generated by this YS program: https://github.com/ingydotnet/yamlscript-vs-rosetta/blob/main/bin/ys-vs-rc which uses that kind of templating with literals quite a bit.
Note that you can avoid the interpoation easily by switching to "data mode" (with ::)
$ cat interpolate2.ys
!yamlscript/v0
say: |
Hello
$(ENV.USER:uc1)
say:: |
Hello
$(ENV.USER:uc1)
$ ys interpolate2.ys
Hello
Ingy
Hello
$(ENV.USER:uc1)Make sense, @delon?
That does! I’m out and about at the moment, but I’ll try it out when I get home.
The :: data mode is helpful. I was wondering what that was.
@ingy Also, do you know if anyone has started on a yamlscript mode for Emacs?
Not to my knowledge. Just looking for syntax highlighting?
Yeah for now
I've noticed PowerShell does a pretty good job in vscode 😂
Hehe
I could see that
YAML mode is meh for YS
Yeah
I’m noticing that too
Does emacs work with LSP servers?
Yes
oh cool. then I plan to have one soon
Ok, cool
I’m trying out powershell for now
do you have any experience with how they work?
LSP...
Not much, though I do have an interest in the subject
the YS compiler has access to input position (line,col) and produces an AST internally so I think it shouldn't be that hard. Maybe we can collab on it.
It would also work well for plain YAML (which is also YS)
That’d be cool
We’ll see how my time goes. But I’d love to collab on this sort of thing
It's sorely missed I know..
I have sometime now, but it may dry up soon
aye
Let me know if you need help with your jekyll thing
For me it’s not really too bad, but I’m a weirdo that likes making my own programming languages 🤓
Yes
I might even have time to pair this afternoon if you wanna
layouts =:
"%":
default: |
$(slurp("./_layouts/default.html"))We’ll see I have a meeting kind of late afternoon
I have a meeting in 5 mins, but free after
Ok, I’ve got a couple of things to take care but that may work
@delon given: https://gist.github.com/ingydotnet/ecfb144b1d86ae32f749e1fc53e7bece
$ ys layouts1.ys
Runtime error:
Could not resolve symbol: default
$ ys layouts2.ys
'%':
default: |+
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{ page.title }}</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<nav>
<a href="/">Home</a>
<a href="/blog/">Blog</a>
</nav>
<h1>{{ page.title }}</h1>
<section>
{{ content }}
</section>
<footer>
© to me
</footer>
</body>
</html>
$ ys layouts3.ys
'%':
default: |
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{ page.title }}</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<nav>
<a href="/">Home</a>
<a href="/blog/">Blog</a>
</nav>
<h1>{{ page.title }}</h1>
<section>
{{ content }}
</section>
<footer>
© to me
</footer>
</body>
</html>
$ I think that's where you were going.
1. You need the !yamlscript/v0 at the top
2. That starts you in "code mode" (where unquoted scalars are ys exprs)
3. :: switches the following value node to "data mode" (where unquoted scalars are normal YAML (strings usually))
4. no need to slurp in the context of a literal scalar unless you are using the slurp as part of a bigger string.
5. slurp(file) can be written in various other syntax forms like:
a. (slurp file)
b. slurp: file
c. slurp file:
d. file.slurp()
e. file:slurp
f. !:slurp file
6. Prefer single quoted strings 'foo' over double quoted "foo" unless using interpolation, escaping or embedded single quotes "foo\n'$bar'"
Also interpolation syntax has 4 distinct forms:
"foo $(slurp(file)) bar"
"foo $slurp(file) bar"
"foo $file bar"
"foo${file}bar"@danielmartincraig you might enjoy this stuff ^^
Sorry, I missed you today, I got a bit behind on things so my time disappeared on me 😅. But thanks for the feedback that looks like what I needed. I’ll dig into it a little more between tomorrow and next week.