Including variables in jekyll from given file - yaml

I want to have my variable assignments placed in a single txt file or similar in jekyll, and have them included in my Front Matter for assignment. So instead of this:
---
variable1: abc
variable2: 123
---
I can have this:
---
{% include variables.txt %}
---
The purpose of this is to have a website that is editable by a technologically inexperienced client. I want to abstract out the core site structure from the things that need to be edited as much as possible - without setting up a CMS, or moving into a dynamic site (so it can still be hosted on github pages)

You could just throw all of your variables into your _config.yml file and then they can be accessed like this:
{{ site.variable1 }}

Related

Which module to use to edit files - Ansible

I want to edit the configuration file of telegraf(system metrics collecting agent).
Telegraf comes in with a default config file which can be edited. There are many input and output plugins defined in there, which are commented out and can be added by removing the comments and also be customized.
I want to edit only some of the plugins defined there, not all of them. For example, consider this is the file,
[global]
interval='10s'
[outputs.influxdb]
host=['http://localhost:8086']
#[outputs.elasticsearch]
# host=['http://localhost:9200']
[inputs.netstat]
interface='eth0'
Now, I want to edit the 3 blocks, global, outputs.influxdb and inputs.netstat. I don't want to edit outputs.elasticsearch but also want that the block outputs.elasticsearch should remain in the file.
When Using Ansible, I firstly used Template module, but if I use that, then the commented data would be lost.
Then I used the ini_file module, instead of editing the already present block, it adds a new block even if it is already present, and results in something like this,
[outputs.influxdb]
host=[http://localhost:8086]
[outputs.influxdb]
host=[http://xx.xx.xx.xx:8086]
Which module is ideal for my scenario ?
There are several options, depending on your purpose.
The lineinfile - module is the best option, if you just want to add, replace or remove one line.
The replace - module is best, if you want to add, replace or delete several lines.
The blockinfile - module can add several lines, surrounded by markers.
If you only want to change two or three lines, you could use as many calls of lineinfile. To change a whole config file, I would recommend, like the commenters suggest, use the template - module.
Ok, if you really really want to avoid using templates, you could try to use replace and a regex like this:
- hosts: local
tasks:
- replace:
path: testfile
regexp: '^\[{{ item.category }}\]\s(.*)host(.*)$'
replace: '[{{ item.category }}]\n host=[{{ item.host }}]'
with_items:
- { category: 'outputs.influxdb', host: 'http://cake.com:8080' }
This, in its current form, would not necessarily handle more than one option under each category, but the regex can be modified to handle multiple lines.
As required, it will not touch the # commented lines. However, if you decide to enable some of the previously inactive sections, you might end up with a slightly messier configuration file that would include the instructions both commented and uncommented (shouldn't impact functionality, only 'looks'). You will also need to account for options that look like the example below (interleaved commented/uncommented values) and create regexes specially for those use-cases:
[section]
option1=['value']
# option2=['value']
option3=['value']
It highly depends on your use-case, but my recommendation remains that templates are to be used instead, as they are a more robust approach, with less chances of things going wrong.

reStructuredText Abstract appears in front of title, otherwise not recognized

Intro
I use (Python) Sphinx to create my personal homepage. It is a collection of technical articles and lately I notices that I write articles that are more of a blog type. Still I like to have a hierarchy other than a linear timeline of posts.
I was trying to get a RSS feed plugin to work and realized that this needs proper document meta data to work properly. There are bibliographic fields and there it says:
When a field list is the first non-comment element in a document (after the document title, if there is one), it may have its fields transformed to document bibliographic data.
First Try
So I assumed that I can do the following and get it right:
.. Copyright © 2014-2016 Martin Ueding <dev#martin-ueding.de>
###################################
The Idiosyncrasies of Bash's quotes
###################################
:Date: 2014-07-13 00:00:00
:Abstract:
The Bash shell has many quirks and takes a lot of time to master. The Fish
shell has a cleaner syntax but is not installed on many systems. The quote
idiosyncrasy of Bash is presented.
Rendering this gives the following:
Missing is the output of the document meta data. I added some little snippet to the template:
{% if meta is defined %}
<p>
{% for key, val in meta.items() %}
{{ key }} → {{ val }} <br />
{% endfor %}
</p>
{% endif %}
{% block body %} {% endblock %}
The Date and Abstract fields just get converted to a table. This is okay-ish for humans but does not help much in feed generation as I need a hard date.
Second Try
So perhaps my interpretation of “after the document title, if there is one” is wrong. So I now did the following:
.. Copyright © 2014-2016 Martin Ueding <dev#martin-ueding.de>
:Date: 2014-07-13 00:00:00
:Abstract:
The Bash shell has many quirks and takes a lot of time to master. The Fish
shell has a cleaner syntax but is not installed on many systems. The quote
idiosyncrasy of Bash is presented.
###################################
The Idiosyncrasies of Bash's quotes
###################################
And there one sees that the meta data is picked up nicely:
However, the abstract is in front of the title! This is a deal-breaker as this just does not look right. I like that my actual div.abstract CSS styling is now used.
Tried So Far
A workaround would be to just move the Date field up such that the RSS feed extension can pick up the date. Then in the theme I would have to somehow put in the date on the page. Or I duplicate it such that it is in a human-readable form in the table below the title and another copy before the title. This way I could control when updates to the RSS feed happen.
Alternatively I could add the title again in the template because I have the title variable there even if the title comes below this meta data table. Then I would need some CSS to remove the second <h1> from the page such that it looks like I want it. But that seems like a kludge and will break without CSS (which I guess is not too much of a requirement, otherwise the title will be duplicated).
Removing the copyright comment does not change anything, either.
Open Question
Is there a better way? Can I have the title first and still get Sphinx to pick up the meta data correctly?

Just show up some posts on a jekyll blog if a YAML Frontmatter variable is set to a specific value

I am currently working on a static blog based on Jekyll and GH-pages.
At the top of my post overview site I do have a section where I would like to place some featured blogposts.
I could probably add the value "featured" to the "tags" in the YAML Front Matter of those posts and insert the line:
{% for post in site.tags.featured %}
Nevertheless I am one of those complicated guys who don't like to stick to the first solution that came in mind (although it probably might be the easiest one).
My idea was to add a new variable featured to my YAML Front Matter and label with the values yes or no (same thing here: yes, I do know that true and false would be easier but I like to be able to transfer the solution to another problem) if it is a featured content (and should be shown in this section) or if it is not.
That might be an easy solution for a jekyll expert but I am pretty new to that kind of static site generator and would love to hear your ideas.
If you assign featured: true or featured: yes, this filter will work :
{% assign featuredPosts = site.posts | where:"featured", true %}
A {% for post in featuredPosts %} will then do the trick.
Note : all the Truthy and Falsy in Liquid are not working in the actual Jekyll.

Including markdown inside yaml front-matter

One of my web pages needs to include rows of items (image, title, description). The description must accept markdown. I haven't found any way to do this in Jekyll without plugins or creating multiple files, one for each item.
Another requirement is that the site be built by Github Pages. ie: no Jekyll plugins, Redcarpet markdown.
Ideally, I would have liked to create a Jekyll data file (_data/products.yml) which contains a structure similar to below. Note that Description contains markdown list and formatting.
- Name: Company A
Year: 2005
Description: >
I was responsible for the following:
- Review of contracts
- Hiring
- Accounting
- Name: Company B
Year: 2010
Description: >
My role included **supervising** the marketing team and leading **publicity**.
Another option I saw was to use Front-matter with the above info. It is slightly more cumbersome since it ties the data with a particular page (eg: work-experience.md).
I've tried various variations on the above but the formatting is never transformed into HTML. How can this be handled?
If you do not wish to use Plugins, I believe the best bet is to have it in _data although not sure if it would be valid YAML or even a valid YAML is a requirement for _data content.
Have you tried using markdownify function such as
{{ site.data.products.description | markdownify }}
http://jekyllrb.com/docs/templates/

Set global variables inside a Jekyll template

I have the following defined in my Jekyll config matter:
dir: a-directory/
I now want to have:
dir: a-directory/
images: {{ dir }}images/
However this won't work. One solution is to place this in my template file
{% capture images %}{{ site.dir }}images/{% endcapture %}
The variable images is now available to other points in that file. However it isn't available to any content being compiled in with that file, e.g my actual pages.
Doing {% capture site.images %} would seem the way to sort that, but you can't assign items to the site or page globals outside of the _config and front matter respectively.
Is it possible to achive this kind of global variable stacking?
(please avoid solutions involving changing my directory structure; if there are similar compilers offering more features without a huge change to workflow I'm open)
It seems YAML doesn't directly support concatenation. There are a few workarounds, though:
https://stackoverflow.com/a/23212524
https://stackoverflow.com/a/22236941

Resources