How to highlight Markdown code in Jekyll? - syntax-highlighting

I want to highlight markdown code in jekyll but I can't find a way how to do it. I looked at Pygement's lexers but I did not found one for markdown or kramdown.
I can highlight other codes but not markdown and kramdawn.

You can use Rouge or Coderay for this purpose.

You could use something like this:
```markdown
{% raw %}{% if page.tags %}...{% endif %}{% endraw %}
```

Related

Transform ansible yml files "with vars templating" to yml files "without templating"

I have this yml files inside an ansible project with templating and vars :
custom_values:
postgresql:
postgresqlDatabase: "{{ secrets.db_name }}"
postgresqlPassword: "{{ secrets.postgres_password }}"
I search a solution to generate the same yml file without the templating like :
custom_values:
postgresql:
postgresqlDatabase: "mydatabase"
postgresqlPassword: "mypassword"
Do you know an existing software to do that automatically ?
You already have a set of ansible templates, that are rendered by a render engine like Jinja2.
The easiest way to convert them would be to actually use the render engine to render the templates, supplying the correct values to it.
You'll end up with a bunch of templates that have the {{ something }} blocks, replaced with the values you want.
Since this looks to be simple Jinja2 templating, please refer to: https://jinja.palletsprojects.com/en/2.10.x/
You'll end up with something like this:
>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')
Please also refer to this stackoverflow post: How to load jinja template directly from filesystem
That explains how to load templates from files
This python code is OK for me :
#import necessary functions from Jinja2 module
from jinja2 import Environment, FileSystemLoader
#Import YAML module
import yaml
#Load data from YAML into Python dictionary
config_data = yaml.load(open('./my_vars.txt'))
#Load Jinja2 template
env = Environment(loader = FileSystemLoader('./templates'), trim_blocks=True, lstrip_blocks=True)
template = env.get_template('my_template.yml')
#Render the template with data and print the output
print(template.render(config_data))
thanks :)

Use Twig variable in include path

I want to use a Twig variable in a include line, but it isn't working:
{% include 'folder/{{ component }}.twig' %}
I get the error:
Unable to find template "folder/{{ component }}.twig"
Can anyone help me out?
Thanks!
I just found the answer here:
https://groups.google.com/forum/#!topic/twig-users/zfnxC16gHx0
The correct syntax is:
{% include 'folder/' ~ component ~ '.twig' %}

Jekyll Liquid - accessing _config.yml dynamically

For internationalizing my app I need to be able to dynamically access entries in a YAML file.
It is best explained with an example:
Page:
---
layout: default
title: title_homepage
---
This will then allow access to the title_homepage variable in the Default Layout Template:
Default Layout:
page.title = "title_homepage"
Now normally I would access my _config.yml file like this:
{{ site.locales[site.default_locale].variable }}
However, now for this to work, I need to access the _config.yml with the value of page.title. This will not work:
{{ site.locales[site.default_locale].page.title }}
I need the following (pseudo code):
{{ site.locales[site.default_locale].#{value of page.title}}
With the way your vars are set, it would be something alog the lines of
{{ site.locales[site.default_locale][page.title] }}
The thing is, ... I don't really see the point of doing this. Let's say your page is the english page. The locale should then be defined within the page, and so should your title!
---
locale: en
title: My Wonderful Page
---
Which you can retrieve with {{ page.title }} ...
What could be the point of putting the title into the _config.yml file?
(edit) well unless you want to access page.title when in another page/post, in this case you have no choice but to put it into _config.yml.

Ruby/Slim: parse Markdown from a YAML file

Been struggling for a while with some YAML parsing inside a Slim template.
my YAML file contain
shortdesc: >
markdown:
if you want to up the feelgood factor Cuban style, then this Monday night at The Buffalo Bar is for you...
But when I output the shortdesc node in my template it's displayed as a string and not interpreted. ("markdown: if you....")
Is there a way to parse the YAML output string to interpret the markdown code?
If I try
p
markdown:
= shortdesc
the template doesn't understand the call to the variable containing the YAML node.
Is that even possible?
It depends on the Markdown Library that you are using.
In BlueCloth, it would be something like this:
= BlueCloth.new(shortdesc).to_html
Yes it's possible. Just need to use interpolation:
p
markdown:
#{shortdesc}

How can I automatically escape HTML content using Jekyll and Markdown?

In foo.markdown I have the following:
---
layout: default
title: Snarky little Ewok
---
A little Ewok is sometimes referred too as <h3>. But pappa Ewok is called <h1> - if you know what's good for you.
Well, I want Jekyll to automatically html escape the greater than and less than characters. I'm seriously fatigued after today's apprentice training and I'm just too lazy to manually html escape myself: >h3<
Is there a config option or something to automatically escape Jekyll markdown content?
If you used textile instead of markdown, there would be a way.
Liquid markup has textilize & escape filters; those two would allow you to do what you wanted, but on textile. You would have to save your files as text (file extension: txt), and then escape the html before textilizing:
---
layout: default
title: Snarky little Ewok
---
This file's extension is .txt
A little Ewok is sometimes referred too as <h3>. But pappa Ewok is called <h1> - if you know what's good for you.
Then on the default.html layout, instead of having:
{{ page.content }}
You would have this:
{{ page.content | xml_escape | textilize }}
Since there's no 'markdownify' filter on Jekyll yet, you can't do that with markdown. There's an issue (Issue 134) on Jekyll for adding a markdownify filter.
EDIT:
It's now possible to use markdown (since jekyll 0.10.1)
{{ page.content | xml_escape | markdownify }}

Resources