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.
You can also define macros:
- inside your document, using the
macro:macro. - Using the
includemacro specifying the path to an.rbfile containing macro definitions stored in thelib/directory (useful especially when compiling single Glyph files).
This is the source code of a fairly simple macro used to format a note:
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.
@name instead of just “note”?For the note macro, it absolutely makes no difference. However, by using @name it is
possible to re-use the same code for the tip, important and caution macros as
well, which are in fact only aliases of the note macro.
The following table lists all the instance variables that can be used inside macros:
| Variable | Description |
|---|---|
@node
|
A
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:
The HTML representation of the note macro is defined in the
macros/reps/html.rb file as follows:
The HTML5 representation of the note macro, on the other hand, is defined in the macros/reps/html5.rb
file as follows: