Glyph - Defining Custom Macros

Glyph was created wih extensibility in mind. You can freely extend Glyph Language by creating or overriding macros, to do whatever you like. Macro definitions are written in pure Ruby code and placed in .rb files within the lib/macros/ folder of your project.

This is the source code of a fairly simple macro used to format a note:

1macro :note do
2  %{<div class="#{@name}"><span class="note-title">#{@name.to_s.capitalize}</span>
3    #{@value}
4
5    </div>}
6end

The macro method takes a single Symbol or String parameter, corresponding to the name of the macro. In this case, the entire block (or body of the macro) is a String corresponding to what we want the macro to evaluate to: a <div> tag containing a note.

The body of the macro is evaluated in the context of the Glyph::Macro class, therefore its instance variables (like @name or @value) can be used directly.

The following table lists all the instance variables that can be used inside macros:

Variable Description
@node

A Glyph::MacroNode containing information about the macro. Useful for accessing parent and child macros, and the current Glyph::Document. Normally, instances of the MacroNode class contain the following keys:

  • :name, the name of the macro.
  • :source, a String identifying the source of the macro (a file, a snippet, etc.)
  • :value, the value of the macro (populated after the document has been parsed and analyzed).
  • :escape, whether the macro is a quoting macro or not.
  • :document, the instance of Document the macro is contained in (populated after the document has been parsed and analyzed).

Note that the first two keys can also be accessed via instance variables.

@name The name of the macro.
@source_name A String identifying the source of the macro (a file, a snippet, etc.).
@source_topic A String identifying the source topic of the macro.
@source_file A String identifying the source file of the macro.

Representations

There’s a small problem with the code used to define the note macro in the previous section: what if I want to format notes using HTML5 instead of HTML, or another output format?

Glyph supports different output formats, therefore macros must be format-independent! In fact, this is the actual source of the note macro:

1macro :note do
2  @data[:name] = @name
3  @data[:text] = value
4  render
5end

The HTML representation of the note macro is defined in the macros/reps/html.rb file as follows:

1rep :note do |data|
2  css_class = data[:name].to_s.match(/[a-z0-9_-]/i) ? data[:name] : "note"
3  %{<div class="#{css_class}">
4<span class="note-title">#{data[:name].to_s.capitalize}</span>#{data[:text]}
5
6</div>}
7end

The HTML5 representation of the note macro, on the other hand, is defined in the macros/reps/html5.rb file as follows:

1rep :note do |data|
2  css_class = data[:name].to_s.match(/[a-z0-9_-]/i) ? data[:name] : "note"
3  %{<aside class="#{css_class}">
4<span class="note-title">#{data[:name].to_s.capitalize}</span>#{data[:text]}
5
6</aside>}
7end
Note the different tags used to render the note.