English 中文(简体)
Extending Jekyll and Liquid to parse post s content
原标题:

My blog, powerred by Jekyll, serves out a Atom feed.

---
layout: nill
rooturi: http://stefan.artspace44.com
---

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

...

{% for post in site.posts %}
  <entry>
    <title>{{ post.title }}</title>
    <link href="{{ page.rooturi }}{{ post.url }}" />
    <updated>{{post.date | date_to_xmlschema }}</updated>
    <id>{{ page.rooturi }}{{ post.id }}</id>
    <content type="html">{{ post.content | xml_escape }}</content>
  </entry>
 {% endfor %}
</feed>

I need to change the content of each post, so that

<img href="/images/01.jpg" />
<a href="/2010/post/">Post</a>

becomes:

<img href="http://stefan.artspace44.com/images/01.jpg" />
<a href="http://stefan.artspace44.com/2010/post/">Post</a>

I was thinking of doing something along the lines of

<content type= html >
  {{ post.content | make_hrefs_base page.rooturi }}
</content>

Where would I code this in jekyll or liquid, and how could I solve the problem of changing only the href values that point to "/" and not "http://otherdomain.com/"?

Thank you

最佳回答

Where would I code this in jekyll or liquid?

In the recently-released Jekyll 0.6.0, you can create your own plugins, including Liquid tag plugins. You can check out the Jekyll plugin documentation for more info, but that would be your best bet.

How could I solve the problem of changing only the href values that point to "/" and not "http://otherdomain.com/"?

Seems pretty easy. In your custom Liquid tag, check to see if the first character is a / ; if it is, then prepend your new domain. You could probably use a Ruby HTML parser to find all instances of <a>, and then change the href attributes as appropriate.

问题回答

I had the same problem in the feed of my blog, and I managed to solve it without using a plugin, i.e. only with vanilla Liquid.

In my Atom XML file, my content is populated like this:

<content type="html">
    {{ post.content | replace: site.feed_linkurl_find, site.feed_linkurl_replace | replace: site.feed_imgurl_find, site.feed_imgurl_replace | xml_escape }}
</content>

...and I have these variables in my config file:

# URL replacements for feeds
feed_linkurl_find: href="/
feed_linkurl_replace: href="http://christianspecht.de/
feed_imgurl_find: src="/
feed_imgurl_replace: src="http://christianspecht.de/

In other words, I just do two ordinary string replaces, one for links and one for images.

The trick is:
In both cases, I replace href="/ by href="http://christianspecht.de/, so only those links that actually begin with / are affected.





相关问题
Using liquid with haml

I m planning to develop a CMS with ruby/rails. One of the main key features that I m planning is to give the user to edit their layout (I m planning to do this through liquid) meanwhile i have red ...

Extending Jekyll and Liquid to parse post s content

My blog, powerred by Jekyll, serves out a Atom feed. --- layout: nill rooturi: http://stefan.artspace44.com --- <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/...

Liquid templates - accessing members by name

I m using Jekyll to create a new blog. It uses Liquid underneath. Jekyll defines certain "variables": site, content, page, post and paginator. These "variables" have several "members". For instance, ...

How to make will_paginate work with liquid

I managed to make a little hack to make will_paginate work with liquid: http://gist.github.com/426737 I wonder if this is safe? Ideas?

undefined method `call for LiquidView:Class

I trying to use Liquid template engine plugin , but I m getting the following error while controller tries to render a .liquid template . "undefined method `call for LiquidView:Class" ...

Liquid Templates Not Parsing!

Im trying to use Liquid Template Language in my Rails Application, i ve watched Ryan Bates video over at rails cast, i pretty much follow the instructions but it just doesnt seem to work! When I try ...

Having difficulties with Jekyll / Liquid

I m tring to do a loop for Nav links below my posts. This is going into the _layout of posts.html I can t get the link to not show if the post is the last or the first. Any help would be awesome. {% ...

热门标签