scratch/content/html/en/blog/2010-10-26-LaTeX-like-macro-and-markdown.md
Yann Esposito (Yogsototh) 6f1f16f2ee Updated blog
2011-03-21 14:42:28 +01:00

2.6 KiB

isHidden menupriority kind created_at title author_name author_uri macros
false 1 article 2010-10-26T14:30:58+02:00 LaTeX like macro for markdown Yann Esposito yannesposito.com
test latex
This is a macro test <span style="text-transform: uppercase">L<sup style="vertical-align: 0.15em; margin-left: -0.36em; margin-right: -0.15em; font-size: .85em">a</sup>T<sub style="vertical-align: -0.5ex; margin-left: -0.1667em; margin-right: -0.125em; font-size: 1em">e</sub>X</span>

begindiv(intro)

<%= tldr %> I made a simple macro system for my blog. Now I juste have to write %latex and it show as %latex.

enddiv

I added a macro system for my blog system. When we are used to %latex this lack can be hard to handle. Particularly when using mathematical notations. In the header of my files I simply write:

macros: test: "This is a macro test" latex: 'LaTeX'

In the body it will replace every occurrence of:

  • %test by %test,
  • and %latex by %latex.

The source code is really simple. For nanoc user, simply put this file in your lib directory.

# usage: # --- # ... # macros: # test: "passed test" # --- # ... # Here is a %test. # class Macros < Nanoc3::Filter identifier :falacy attr_accessor :macro def initialize(arg) super @macro={} @macro[:tlal] = %{tlàl : } @macro[:tldr] = %{tl;dr: } if @item.nil? if not arg.nil? @macro.merge!( arg ) end else if not @item[:macros].nil? @macro.merge!( @item[:macros] ) end end end def macro_value_for(macro_name) if macro_name.nil? or macro_name=="" or @macro[macro_name.intern].nil? return %{%#{macro_name}} end return @macro[macro_name.intern] end def run(content, params={}) content.gsub(/%(\w*)/) do |m| if m != '%' macro_value_for($1) else m end end end end

Macros could be very useful, read this article for example.