how to write code blocks using maruku - ruby

how can i write code blocks in maruku
for ruby,javascript
currently i am using technique. but my first line moving to left.
hash["test"] = "test"
hash.merge!("test" => "test")
h=HashWithIndifferentAccess.new
h.update(:test => "test")
{:lang=ruby html_use_syntax=true}

I'm not sure I fully understand the question, but Maruku is just a Markdown interpreter.
To produce a code block in Markdown, simply indent every line of the block by at least 4 spaces or 1 tab. For example, given this input:
This is a normal paragraph:
This is a code block.
Markdown will generate:
<p>This is a normal paragraph:</p>
<pre><code>This is a code block.
</code></pre>

I add this answer because I ended up here searching for a solution to code blocks with Maruku using Jekyll. For anyone else in the same boat, use the Liquid tags for code blocks instead of the Markdown syntax:
{% highlight java %}
System.out.println("Hello, Maruku.");
{% endhighlight %}
Also see this question/answer: Highlight with Jekyll and pygments doesn't work

Related

Pandoc: complete pass through from tex to html?

I'm using pandoc to convert TeX files into HTML files (to be used with JeKyll).
I want to insert some raw block directly into the TeX file in a way that it survives, without any alteration, the conversion from TeX to HTML.
For instance, I might want to add something like this:
{% highlight python %}
def func(ok):
return ok
{% endhighlight %}
I can do this from md to HTML by using {=html}, but what about the TeX->md part?
This requires the use of a filter, as #mb21 pointed out alreday.
You'll probably want the input document to remain valid LaTeX, so a good method would be to use a specially marked verbatim environment, like so:
\begin{verbatim}
%%%HTML
<aside>Embedding raw HTML can be helpful</aside>
\end{verbatim}
Pandoc will read this as a normal code block, but we can use a filter to convert it into a raw HTML block:
function CodeBlock(cb)
local rawHtml = cb.text:match('^%s*%%%%%%HTML\n(.*)')
if rawHtml then
return pandoc.RawBlock('html', rawHtml)
end
end
Save the above into a file and use it as the argument of pandoc's --lua-filter option.

Prevent double escaping with CodeRay and RDiscount

I am looking for the most straight forward way to have code syntax highlighting in markdown, using Ruby (without Rails).
I have tried some things with Kramdown and Rouge, and could not make it work, so I am now working with RDiscount and CodeRay.
Most of the things work as I expect, with one small and one big issue:
Small Issue: The only way I found to make CodeRay work with RDiscount, is by applying the highlighting on the HTML rather than on the markdown document. This seems a little off to me and prone to errors. Is there another way?
Big Issue: I am now facing a double HTML escaping issue, and was unable to find any html_escape: false option in the CodeRay documentation.
Code
require 'rdiscount'
require 'coderay'
markdown = <<EOF
```ruby
A > B
```
EOF
def coderay(text)
text.gsub(/\<code class="(.+?)"\>(.+?)\<\/code\>/m) do
CodeRay.scan($2, $1).html
end
end
html = RDiscount.new(markdown).to_html
html = coderay(html)
puts html
Output
Notice the double escaping on the greater than sign:
<pre>
<span class="constant">A</span> &gt; <span class="constant">B</span>
</pre>
I have found this related question, but its old and without a solution for this case.
The only way I can come up with, is to unescape the HTML before passing it through CodeRay, but this does not feel right to me. Nevertheless, a working alternative below:
def coderay(text)
text.gsub(/\<code class="(.+?)"\>(.+?)\<\/code\>/m) do
lang, code = $1, $2
code = CGI.unescapeHTML code
CodeRay.scan(code, lang).html
end
end

Jekyll: including a post inside another post

Does Jekyll provide a way to include a post inside another post? I know that sounds a bit goofy, but I'm using it for a cooking/recipe site. Some of my recipes are made up of components, or other, smaller recipes.
I'm looking for a way to include a handful of posts inside another post, complete with template and all. Is this possible?
I did a ton of googling, and found I was only able to include into a page, not post, and it didn't use the default liquid template, just plain markdown.
Ideas?
I used Jekyll's Collections in my website to solve a problem similar to yours.
Collections are still an experimental feature in Jekyll, and the solution I used is pretty hacky by itself, but if you don't go too crazy with it it's manageable enough.
For a more detailed and friendly intro to collections, here's a great post about getting started with them.
One thing we need to have in mind is that we should get away from posts for cases like this.
As mentioned by Ben Balter in the link above, “If people are using blog posts for a non-blog post thing, Jekyll has already failed”. Recipes are not posts, and we should avoid using them as such if we can.
With that said, here's my approach in three steps:
Create two collections (our custom post types) — let's say these are “Recipes” and “Ingredients”
Find a way to relate them to each other — To make sure “lettuce” will be listed under “salad”
Include the relevant “Ingredients” in your “Recipes” — Adding the liquid code to actually display information from “lettuce” somewhere in the “salad” page.
Step 1
Create _ingredients and _recipes folders for each of your collections (make sure to name them in plural) and add them to your _config.yml file:
collections:
ingredients:
output: true
recipes:
output: true
permalink: /recipes/:path/
The output: true parameter creates an individual HTML page for each item in the collection and permalink (optional) lets you control the URL.
Step 2
Add a piece of metadata like this to the YAML front-matter of your lettuce.md collection item:
---
parent-collection: salad
---
If lettuce.md belongs to more than one recipe, you can add more than one. Make sure to add content below the metadata if you need a description:
---
parent-collection:
- salad
- hamburguer
- taco
---
Lettuce is really good for your health, but it kinda sucks as a food.
Note that salad, hamburguer and taco should named exactly like the recipe URL slug or this won't work (i.e. greatrecipes.com/recipes/salad). If you have a recipe name that's a sentence, you wrap it in quotes.
This variable will be slugified later on, so a name like parent-collection: The Amazing Souflè Français will match the-amazing-soufle-francais. Still, weird stuff happens: when in doubt, just write smallcaps salad.
This is the hacky part.
Step 3
Create a specific layout for your recipes (like _layout/recipe-page.html) and add the code below — this is where we'll include the ingredients into the recipe page.
Remember your recipes (salad.md and friends) should be pointing to this layout.
{% capture current_collection %}{{ page.url | remove: "recipes" | remove: "/" | remove: " " }}{% endcapture %}
Here we capture the name of the recipe from the URL and assign it to the current_collection variable. Make sure this in a single line because making it multi-line will add a bunch of whitespace to the captured string and break your code 10/10 times. Insane.
<div class="recipe-content">
<p>{{ content }}</p> -- This is the content of the current recipe in the collection, your "post text".
</div>
{% for ingredient in site.ingredients %}
{% capture captured_parent_collection %}{{ ingredient.parent-collection | slugify }}{% endcapture %} -- This capture is just to pass the slugify filter and sanitize parent.collection to make sure it matches the URL slug
{% if captured_parent_collection == current_collection %}
<div class="recipe-ingredient">
<h3>{{ ingredient.name }}</h3> -- "<h3>Lettuce</h3>"
<p>{{ ingredient.content }}</p> -- "<p>Lettuce is really good for your health, but it kinda sucks as a food.</p>"
</div>
{% endif %}
{% endfor %}
Match the ingredients for this recipe and includes each one of them into the layout. YASS!
Each of your ingredients should be appearing right under your recipe text.
Note that you're not pulling the ingredients inside the recipe text like you described in your question, but including them in the layout after it. As far as I know you can't really add things in the middle of your post/collection text — unless you hack your way through a custom plugin for Jekyll.
There are certainly less hacky ways to do this, but this is how I made it work. Let me know if you find any trouble trying this for yourself — and anyone else, feel free to correct me.
I have this setup going on in my site — take a look at the layout where I pull in my child collection items and the metadata on the child collection items themselves.
I stumbled upon this question while looking for a way to do this, and here's what I ended up doing. I figured I'd post it in case somebody else is looking for a way to do this as a one-off and doesn't feel like creating a whole collection.
In the post where you want to include your other post, you can use Jekyll's where filter to get a reference to your other post, and then print that post's content:
---
layout: whatever
---
Your original post's content here.
{% assign myOtherPost = site.posts | where:"url", "/other/post/url" | first %}
{{ myOtherPost.content }}
Some more content here.

Preventing category downcasing in octopress

I've been using Octopress for a while, but I haven't pulled upstream changes for ages. I just made a new local branch to do so, and the merging was pretty painless, but one problem I have is that my category names are now downcased everywhere. This didn't use to happen, and it's a problem for me because I colorize links differently based on the category of each post, using the following:
{% capture category_class %}
{% for category in post.categories %}
{{ category | prepend:'category-' }}
{% endfor %}
{% endcapture %}
# ...
<div class="{{ category_class | strip_newlines }}">
Here, the category_class ends up being something like "category-coding" even though the category is specified in the post source as Coding, with a capital C. Now, I could just change my SASS to use lowercase category names for the classes... but then my category names would still be lowercase everywhere else, and I'd prefer them not to be.
So I'd like to remove this downcasing of category names throughout Octopress. But I can't for the life of me figure out where it is actually happening. (It probably doesn't help that I don't know Ruby.)
There's a downcase call in Jekyll which produces the lowercased categories:
https://github.com/jekyll/jekyll/blob/v2.5.3/lib/jekyll/post.rb#L83
def populate_categories
categories_from_data =
Utils.pluralized_array_from_hash(data, 'category', 'categories')
self.categories = (Array(categories) +
categories_from_data).map {|c| c.to_s.downcase}.flatten.uniq
end
There doesn't seem to be any other way to access the categories array within a post. You would need to modify or monkey patch Jekyll yourself or just use Javascript or CSS (text-transform) to re-capitalize.
It seems Octopress also supports plugins, so that could be one more solution if you want to do some custom Ruby hacking. I.e. create a capitalized categories plugin.

Spread Liquid `include` over mutliple lines

I have an "included" template with several parameters. The contents of the parameters get a bit muddled if I cram them all into a single line, so I would prefer something like this:
{% include product_details
weight= "5.8lbs (2.6 kg)"
width= "22" (56cm)"
length= "49" (125cm)"
thickness= "1¼" (3cm)"
case= "MT51413"
%}
However, this gives me the following error when generating the site:
error: Tag '{%' was not properly terminated with regexp: /\%}/.
Is there any way to spread a Liquid include over several lines?
Sorry you can't, the liquid parser expects the whole statement in one line, this is true for all statements not only for include, try to split an if statement and you receive the same error

Resources