I'm trying to extract the file name from a url on Jekyll using this ruby snippet:
{% assign filename = page.url.split('/')[-1] | replace: '.html', '.md' %}
If I just use:
{% assign filename = page.url | replace: '.html', '.md' %}
I get back the url with the replaced file type but my .split('/')[-1] doesn't seem to work.
I tried running the following in standalone ruby to ensure my syntax was right, and it returned bird as expected:
"cat/dog/bird".split('/')[-1]
Why doesn't the same syntax work in my Jekyll instance? Is it that page.url isn't a string, or something else?
The problem is mixing ruby code with Liquid tags.
To extract the filename from a url in Jekyll you can use just pure Liquid template filters, using the equivalents of what you tried:
.split('/') -> | split: '/'
[-1] -> | last
As an example with a custom URL:
{% assign url_example = "cat/dog/bird.html" %}
{% assign filename = url_example | split: '/' | last | replace: '.html', '.md' %}
{{filename}}
outputs:
bird.md
Related
I have pillar that is supposed to have value of "-" (without the quotation).
I tried:
my_pillar: -
my_pillar: '-'
my_pillar: "-"
my_pillar: {% raw %}"-"{% endraw %}
{% raw %}my_pillar: "-"{% endraw %}
None of those worked and I am getting error: "block sequence entries are not allowed in this context".
I understand what the problem here is, Salt thinks that it is part of YAML definition of values, not the value itself, but how do I work around that?
As OrangeDog mentions, any type of quoting will work:
docker run --rm -it saltstack/salt /bin/sh
mkdir -p /srv/pillar
cat <<EOF>/srv/pillar/top.sls
base:
'*':
- stuff
EOF
echo "my_pillar: -" >/srv/pillar/stuff.sls
salt-call --local pillar.items -ldebug
# Error happens ^^^^^^^^^^^^^^^^^^^^^^
echo "my_pillar: '-'" >/srv/pillar/stuff.sls
salt-call pillar.items
The output for me:
local:
----------
my_pillar:
-
I'm trying to run jekyll build on GitLab CI.
This is my .gitlab-ci.yml:
pages:
script:
- export LC_ALL=en_US.UTF-8
- export LANG=en_US.UTF-8
- gem install jekyll
- jekyll build --destination public
artifacts:
paths:
- public
When the task runs I get this error:
Generating...
Liquid Exception: invalid byte sequence in US-ASCII in documentation.html
jekyll 3.1.2 | Error: invalid byte sequence in US-ASCII
ERROR: Build failed with: exit code 1
More informations
documentation.html is:
---
layout: page
title: Documentation
description: Learn how to create awesome poppers
---
<!-- This page is generated by the grunt doc task of the master branch! Don't edit it! -->
{% markdown documentation.md %}
And documentation.md is a markdown document generated by grunt-jsdoc2md.
This is the markdown plugin I'm using:
=begin
Jekyll tag to include Markdown text from _includes directory preprocessing with Liquid.
Usage:
{% markdown <filename> %}
Dependency:
- kramdown
=end
module Jekyll
class MarkdownTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
#text = text.strip
end
require "kramdown"
def render(context)
tmpl = File.read File.join Dir.pwd, "_includes", #text
site = context.registers[:site]
tmpl = (Liquid::Template.parse tmpl).render site.site_payload
html = Kramdown::Document.new(tmpl).to_html
end
end
end
Liquid::Template.register_tag('markdown', Jekyll::MarkdownTag)
Attempts
As you see, I've already tried setting LC_ALL and LANG to en_US.UTF-8.
I've also added encoding: utf-8 to my _config.yml but it still doesn't work...
Another attempt was to use #text = text.encode("iso-8859-1").force_encoding("utf-8").strip in the markdown plugin.
Suggestions?
I had the same problem and found this solution, which worked for me.
https://gitlab.com/gitlab-org/gitlab-ce/issues/14983
image: ruby:2.3
before_script:
- apt-get update >/dev/null
- apt-get install -y locales >/dev/null
- echo "en_US UTF-8" > /etc/locale.gen
- locale-gen en_US.UTF-8
- export LANG=en_US.UTF-8
- export LANGUAGE=en_US:en
- export LC_ALL=en_US.UTF-8
I deleted the markdown plugin and used this:
---
layout: page
title: Documentation
description: Learn how to create awesome poppers
---
<!-- This page is generated by the grunt doc task of the master branch! Don't edit it! -->
{% capture documentation %}
{% include documentation.md %}
{% endcapture %}
{{ documentation | markdownify }}
Everything seems working fine now.
I have something like this dictionary:
env: qat
target_host: >
{%if env in ['prd'] %}one
{%elif env in ['qat','stg'] %}two
{%else%}three
{%endif%}
when I print it I get:
ok: [localhost] => {
"var": {
"target_host": "two "
} }
So it is converting the \n at the end of the line to a space. Which is exactly what it is supposed to do. However in this case I am just trying to spread out the lines to make the structure of the if/else more readable and I don't want the extra space. It works as expected if I put it all on one line without the > but I would like to be able to make it multiline just so its easier to read.
I found this question
Is there a way to represent a long string that doesnt have any whitespace on multiple lines in a YAML document?
So I could do:
env: qat
target_host: "{%if env in ['prd'] %}one\
{%elif env in ['qat','stg'] %}two\
{%else%}three\
{%endif%}"
And that gives the desired result.
Is there anyway to accomplish this without cluttering it up even more?
In Jinja* you can strip whitespaces/newlines by adding a minus sign to the start/end of a block. This should do the trick:
env: qat
target_host: >
{%if env in ['prd'] -%}one
{%- elif env in ['qat','stg'] -%}two
{%- else -%}three
{%- endif%}
* Jinja 2 is the templating engine used by Ansible.
Maybe what you need is the | literal?
env: qat
target_host: |
{%if env in ['prd'] %}one
{%elif env in ['qat','stg'] %}two
{%else%}three
{%endif%}
This will not 'fold' newlines into spaces, as oposed to >
module MyFilter
def all_caps(input)
input.upcase
end
end
Liquid::Template.register_filter(MyFilter)
template = Liquid::Template.parse(" {{ 'hi john' | all_caps }} ")
template.render
# => " HI JOHN "
template = Liquid::Template.parse(" {{ 'hi {{name}}' | all_caps }} ")
template.render('name' => 'john')
# => " ' | all_caps }} "
How do I fix this? No mentions in the official documentation.
Note: I have used all_caps example just for the simplicity. I want to achieve something complex.
I did figure out a way to do that. Would appreciate if there is any other solution.
template = Liquid::Template.parse("{% capture some_text %} Hi, {{name}} {% endcapture %} {{ some_text | all_caps }}")
template.render('name' => 'john')
Hope this helps.
I've been using Liquid extensions to reformat dates on my Jekyll based site, for example:
<p>{{ post.date | date_to_string }}</p>
This works fine in my index.html page which just takes the five most recent posts and then iterates them post by post. However, this fails when I am trying to render such a date within my _layouts/base.html template.
I have tried:
{{ page.date | date_to_string }}
{{ page.title }} works without issue, and {{ page.date}} renders when I use it without the liquid filter, outputting, for example, 2012-03-12 00:00:00 +0000.
Why does the date_to_string filter fail with the input provided by {{ page.date }}. I get the following error:
Liquid Exception: undefined method `strftime' for nil:NilClass in base
And the build fails. Thanks for any help!
On my site I use
{{ page.date | date: "%d %B %Y" }}
It grabs the date from the markdown file. And is rendered like so:
26 December 2012
See these links for some extra reading:
How does Jekyll date formatting work?
http://liquid.rubyforge.org/classes/Liquid/StandardFilters.html#M000012
EDIT: To answer your question in the comments section:
If you want to use date_to_string you have to call it like this:
{{ site.time | date_to_string }}
There is also {{ site.time | date_to_long_string } which will write the month out in it's full form eg. November not Nov.
Source
https://github.com/mojombo/jekyll/wiki/Liquid-Extensions