----- isHidden: false menupriority: 1 kind: article created_at: 2010-10-26T14:30:58+02:00 title: LaTeX like macro for markdown author_name: Yann Esposito author_uri: yannesposito.com macros: test: "This is a macro test" latex: 'LaTeX' # tags: ----- 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](http://adam.gomaa.us/blog/2007/oct/22/markdown-doesnt-scale/index.html) for example.