glyph 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. data/AUTHORS.textile +1 -1
  2. data/CHANGELOG.textile +119 -222
  3. data/LICENSE.textile +1 -1
  4. data/README.textile +42 -23
  5. data/Rakefile +1 -3
  6. data/VERSION +1 -1
  7. data/benchmark.rb +72 -0
  8. data/book/config.yml +4 -4
  9. data/book/document.glyph +90 -57
  10. data/book/images/document_generation.png +0 -0
  11. data/book/lib/macros/reference.rb +75 -22
  12. data/book/output/html/glyph.html +3183 -2121
  13. data/book/output/html/images/document_generation.png +0 -0
  14. data/book/output/pdf/glyph.pdf +7370 -4913
  15. data/book/resources/document_generation.txt +34 -0
  16. data/book/snippets.yml +6 -0
  17. data/book/text/changelog.glyph +45 -34
  18. data/book/text/compiling/compiling.glyph +23 -0
  19. data/book/text/compiling/lite_mode.glyph +23 -0
  20. data/book/text/compiling/programmatic_usage.glyph +77 -0
  21. data/book/text/extending/bookmarks_headers.glyph +21 -0
  22. data/book/text/extending/further_reading.glyph +13 -0
  23. data/book/text/extending/internals.glyph +79 -0
  24. data/book/text/extending/interpreting.glyph +51 -0
  25. data/book/text/extending/macro_def.glyph +64 -0
  26. data/book/text/extending/params_attrs.glyph +70 -0
  27. data/book/text/extending/placeholders.glyph +34 -0
  28. data/book/text/extending/validators.glyph +16 -0
  29. data/book/text/getting_started/configuration.glyph +49 -0
  30. data/book/text/getting_started/create_project.glyph +41 -0
  31. data/book/text/getting_started/structure.glyph +55 -0
  32. data/book/text/introduction.glyph +49 -26
  33. data/book/text/license.glyph +1 -1
  34. data/book/text/macros/macros_block.glyph +99 -0
  35. data/book/text/macros/macros_core.glyph +208 -0
  36. data/book/text/macros/macros_filters.glyph +40 -0
  37. data/book/text/macros/macros_inline.glyph +50 -0
  38. data/book/text/macros/macros_structure.glyph +100 -0
  39. data/book/text/ref_commands.glyph +94 -73
  40. data/book/text/ref_config.glyph +34 -42
  41. data/book/text/ref_macros.glyph +1 -373
  42. data/book/text/text_editing/code.glyph +51 -0
  43. data/book/text/text_editing/conditionals.glyph +49 -0
  44. data/book/text/text_editing/evaluation.glyph +13 -0
  45. data/book/text/text_editing/glyph_files.glyph +7 -0
  46. data/book/text/text_editing/images.glyph +29 -0
  47. data/book/text/text_editing/inclusions.glyph +44 -0
  48. data/book/text/text_editing/links.glyph +53 -0
  49. data/book/text/text_editing/macro_intro.glyph +111 -0
  50. data/book/text/text_editing/raw_html.glyph +112 -0
  51. data/book/text/text_editing/sections.glyph +63 -0
  52. data/book/text/text_editing/stylesheets.glyph +36 -0
  53. data/book/text/troubleshooting/errors_command.glyph +39 -0
  54. data/book/text/troubleshooting/errors_generic.glyph +29 -0
  55. data/book/text/troubleshooting/errors_intro.glyph +3 -0
  56. data/book/text/troubleshooting/errors_macro.glyph +98 -0
  57. data/book/text/troubleshooting/errors_parser.glyph +29 -0
  58. data/config.yml +77 -58
  59. data/document.glyph +25 -25
  60. data/glyph.gemspec +57 -22
  61. data/lib/glyph.rb +54 -13
  62. data/lib/glyph/commands.rb +84 -17
  63. data/lib/glyph/config.rb +3 -3
  64. data/lib/glyph/document.rb +14 -8
  65. data/lib/glyph/interpreter.rb +18 -58
  66. data/lib/glyph/macro.rb +160 -55
  67. data/lib/glyph/macro_validators.rb +104 -12
  68. data/lib/glyph/node.rb +24 -0
  69. data/lib/glyph/parser.rb +278 -0
  70. data/lib/glyph/syntax_node.rb +225 -0
  71. data/macros/core.rb +212 -0
  72. data/macros/filters.rb +66 -15
  73. data/macros/html/block.rb +43 -105
  74. data/macros/html/inline.rb +11 -12
  75. data/macros/html/structure.rb +123 -58
  76. data/macros/xml.rb +33 -0
  77. data/spec/files/container.textile +2 -2
  78. data/spec/files/document.glyph +2 -2
  79. data/spec/files/document_with_toc.glyph +3 -3
  80. data/spec/files/included.textile +1 -1
  81. data/spec/files/ligature.jpg +0 -0
  82. data/spec/files/markdown.markdown +2 -1
  83. data/spec/lib/commands_spec.rb +46 -3
  84. data/spec/lib/document_spec.rb +4 -4
  85. data/spec/lib/glyph_spec.rb +17 -46
  86. data/spec/lib/interpreter_spec.rb +6 -25
  87. data/spec/lib/macro_spec.rb +141 -43
  88. data/spec/lib/macro_validators_spec.rb +27 -5
  89. data/spec/lib/node_spec.rb +26 -1
  90. data/spec/lib/parser_spec.rb +246 -0
  91. data/spec/lib/syntax_node_spec.rb +111 -0
  92. data/spec/macros/core_spec.rb +195 -0
  93. data/spec/macros/filters_spec.rb +38 -4
  94. data/spec/macros/macros_spec.rb +20 -176
  95. data/spec/macros/textile_spec.rb +13 -71
  96. data/spec/macros/xml_spec.rb +77 -0
  97. data/spec/spec_helper.rb +50 -10
  98. data/spec/tasks/load_spec.rb +13 -2
  99. data/styles/default.css +18 -6
  100. data/styles/pagination.css +1 -19
  101. data/tasks/generate.rake +2 -2
  102. data/tasks/load.rake +27 -17
  103. data/tasks/project.rake +1 -1
  104. metadata +75 -62
  105. data/book/script/compile.rb +0 -8
  106. data/book/script/prof +0 -1
  107. data/book/script/prof_results.htm +0 -21079
  108. data/book/text/authoring.glyph +0 -548
  109. data/book/text/extending.glyph +0 -224
  110. data/book/text/getting_started.glyph +0 -158
  111. data/book/text/troubleshooting.glyph +0 -179
  112. data/lib/glyph/glyph_language.rb +0 -538
  113. data/lib/glyph/glyph_language.treetop +0 -27
  114. data/macros/common.rb +0 -160
@@ -0,0 +1,34 @@
1
+ participant generate_document
2
+ participant Interpreter
3
+ participant Parser
4
+ participant Document
5
+ participant SyntaxNode
6
+ participant Macro
7
+
8
+
9
+ generate_document->Interpreter: new
10
+ Interpreter->Parser: new
11
+ generate_document->Interpreter: parse
12
+ Interpreter->Parser: parse
13
+ Parser-->Interpreter: tree (SyntaxNode)
14
+ Interpreter->Document: new
15
+ generate_document->Interpreter: process
16
+ Interpreter->Document: analyze
17
+ loop Processing
18
+ Document->SyntaxNode: evaluate
19
+ alt if macro node
20
+ SyntaxNode->Macro: expand
21
+ end
22
+ end
23
+ Document-->Interpreter: :analized
24
+ generate_document->Interpreter: postprocess
25
+ Interpreter->Document: finalize
26
+ loop Post-processing
27
+ note over Document
28
+ Replace all escape sequences
29
+ and placeholders in the
30
+ final text output
31
+ end note
32
+ end
33
+ Document-->Interpreter: :finalized
34
+ Interpreter-->generate_document: document
data/book/snippets.yml CHANGED
@@ -10,3 +10,9 @@
10
10
  :only_after_declaration: "can only be used _after_ its declaration"
11
11
  :coderay: "=>[http://coderay.rubychan.de/|Coderay]"
12
12
  :uv: "=>[http://ultraviolet.rubyforge.org/|Ultraviolet]"
13
+ :yardoc: "http://yardoc.org/docs/h3rald-glyph"
14
+ :unsafe: "This macro cannot be used in =>[#modes|safe mode]."
15
+ :bin_params: "-p[0|The first expression to test]-p[1|The second expression to test]"
16
+ :img_file: "The name of the image file (relative to the code[images/] folder)."
17
+ :img_attrs: "Any attribute supported by the =>[http://www.w3schools.com/tags/tag_IMG.asp|img tag]."
18
+ :opt: "em[(optional)]"
@@ -1,54 +1,65 @@
1
1
  %[=
2
- macro :issue do
3
- exact_parameters 2
4
- ident, desc = @params
5
- encode %{
6
- tr[
7
- td[\=>[http://github.com/h3rald/glyph/issues/closed#issue/#{ident}\|##{ident}]]
8
- td[textile[#{desc}]]
9
- ]
10
- }
11
- end
12
-
13
2
  macro :features do
14
3
  verb = (@name == :features) ? "Implemented" : "Fixed"
15
- total = @node.children.length
16
- encode %{
17
- section[header[#{total} #{@name.to_s.capitalize} #{verb}]
4
+ total = @node.children[0].children.length
5
+ interpret %{
6
+ section[
7
+ @title[#{total} #{@name.to_s.capitalize} #{verb}]
18
8
  table[
19
9
  tr[
20
10
  th[ID]
21
11
  th[Description]
22
12
  ]
23
- #@value
13
+ #{@node.value}
24
14
  ]
25
15
  ]
26
16
  }
27
17
  end
28
18
 
29
- macro :release do
30
- exact_parameters 3
31
- number, date, contents = @params
32
- interpret %{
33
- section[header[v#{number} – #{date}]
34
- #{decode contents}
35
- ]
36
- }
37
- end
38
-
39
-
40
19
  macro_alias :bugs => :features
41
-
42
- =]--[
43
- ?[%[lite?]|*[=
44
- %:[%>|"#@value macro"]
45
- %:[#>|"#@value command"]
46
- =]
20
+ =]
21
+ rw:[release|
22
+ section[
23
+ @title[v{{0}} – {{1}}]
24
+ {{2}}
25
+ ]
26
+ ]
27
+ rw:[issue|
28
+ tr[
29
+ td[\.=>[http://github.com/h3rald/glyph/issues/closed#issue/{{0}}|#{{0}}]]
30
+ td[txt[{{1}}]]
31
+ ]
32
+ ]
33
+ ?[%[lite?]|
34
+ %:[%>|"#{value} macro"]
35
+ %:[#>|"#{value} command"]
36
+ %:[$>|"#{value} setting"]
37
+ ]
38
+ release[0.3.0|June 13th 2010|
39
+ features[
40
+ issue[39|A new #>[outline] is available to display the document outline.]
41
+ issue[110|It is now possible to use Glyph language to produce arbitrary XML code.]
42
+ issue[111|System settings are now stored within a @system.*@ namespace and cannot be changed via the %>[config:] or the #>[config].]
43
+ issue[116|It is now possible to use named attributes within Glyph macros.]
44
+ issue[119|#[new_parser]A new parser was implemented from scratch to improve performance. Treetop gem no longer required.]
45
+ issue[121|Some macros have been removed in favor of XML fallback, others have been updated.]
46
+ issue[123|The SyntaxNode class has been specialized to differentiate between macros, attributes, parameters, text and escapes.]
47
+ issue[124|Implemented new %>[article] and %>[book].]
48
+ issue[126|A new %>[rewrite:] has been implemented to create simple macros using just Glyph code.]
49
+ issue[127|A new %>[alias] has been implemented to create macro aliases.]
50
+ issue[128|A blacklist for XML tags has been exposed via the $>[language.options.xml_blacklist].]
51
+ issue[129|The %>[include] can now be used in lite mode, it can evaluate ruby files and requires relative paths.]
52
+ issue[130|A new "safe mode" has been implemented to explicitly forbid certain potentially unsafe macros.]
53
+ ]
54
+ bugs[
55
+ issue[109|Performance has been dramatically improved by implementing a parser from scratch (see =>[#new_parser|#119])]
56
+ issue[122|Macro encoding/decoding no longer necessary due to the new parser (see =>[#new_parser|#119])]
57
+ issue[125|Warning messages have been streamlined.]
47
58
  ]
48
59
  ]
49
60
  release[0.2.0|May 9th 2010|
50
61
  features[
51
- issue[62|A new %>[highlight] is available to highlight source code (CodeRay or UltraViolet requireed).]
62
+ issue[62|A new %>[highlight] is available to highlight source code (CodeRay or UltraViolet required).]
52
63
  issue[76|It is now possible to use Glyph programmatically via the new @Glyph#filter@ and @Glyph#compile@ methods.]
53
64
  issue[87|It is now possible to define snippets inside a Glyph source file using the %>[snippet:].]
54
65
  issue[88|It is now possible to change configuration settings inside a Glyph source file using the %>[config:] (Jabbslad).]
@@ -68,7 +79,7 @@ release[0.2.0|May 9th 2010|
68
79
  issue[101|Additional tests have been developed to improve Textile support. There should no longer be errors when using textile block elements inside Glyph macros.]
69
80
  issue[103|Fixed a bug that caused test failures when deleting the test project directory.]
70
81
  issue[104|Nested Glyph macros calling @Macro#interpret@ no longer ignore escape delimiters.]
71
- issue[107|Added the possibility to encode (using the %>[encode]) and decode (using the %>[decode]) macros so that they can be interpreted later.]
82
+ issue[107|Added the possibility to encode (using the @encode@ macro) and decode (using the @decode@ macro) macros so that they can be interpreted later.]
72
83
  ]
73
84
  ]
74
85
  release[0.1.0|April 8th 2010|
@@ -0,0 +1,23 @@
1
+ section[
2
+ @title[Compiling a project]
3
+ @id[compile]
4
+ p[By default, a Glyph project can be "compiled" into an HTML document. Additionally, Glyph can also be used to produce PDF documents through &[prince], and in future releases more output targets will be supported.]
5
+ section[
6
+ @title[HTML output]
7
+ p[To compile a Glyph project to an HTML document, use the #>[compile] within your Glyph project folder. Glyph parses the code[document.glyph] file (and all included files and snippets); if no errors are found, Glyph creates an HTML document in the code[/output/html] folder.]
8
+ p[The name of the HTML file can be set in the configuration (\.$>[document.filename]).]
9
+ ]
10
+ section[
11
+ @title[PDF Output]
12
+ p[To generate a PDF document, you must specify code[pdf] as format, like this:]
13
+ p[code[= glyph compile -f pdf =]]
14
+ p[The command above will attempt to compile the project into an HTML document and then call Prince to generate a PDF document from it. In order for this to work, you must download and install &[prince]. It's not open source, but the free version is fully functional, and it just adds a small logo on the first page.]
15
+ note[Glyph v\.%[Glyph::VERSION] has been successfully tested with Prince v7.0, and the PDF version of this very book was generated with it.]
16
+ ]
17
+ section[
18
+ @title[Auto Regeneration]
19
+ @id[auto_regeneration]
20
+ txt[You can also call the #>[compile] with a @--auto@ switch. If you do so, your project will be recompiled automatically every time any source file is changed.]
21
+ note[Auto regeneration requires the =>[http://rubygems.org/gems/directory_watcher|directory_watcher] gem to be installed.]
22
+ ]
23
+ ]
@@ -0,0 +1,23 @@
1
+ section[
2
+ @title[Compiling single Glyph files]
3
+ @id[lite_mode]
4
+ p[Glyph's primary goal is to author complex documents like books or manuals. In order to do so, a Glyph project is required to keep everything organized and automated via a set of predefined conventions, exactly like Ruby on Rails or other similar frameworks do.]
5
+ p[If you want to write a one-page article or a short draft, however, creating and managing Glyph projects can be an unnecessary burden. Luckily, you don't have to: you can use Glyph to compile single files containing Glyph code, by adding one parameter (or two if you want to specify a custom destination file) to the #>[compile], like this:]
6
+ p[code[glyph compile source.glyph destination.htm]]
7
+ p[This command will process a file called code[source.glyph] and produce an HTML file called code[destination.htm].]
8
+ section[
9
+ @title[Limitations]
10
+ @id[lite_limitations]
11
+ &:[only_defined_through|can only be defined inside the source file, using the]
12
+ txt[
13
+ &:[referenced_with_path|must be referenced with their absolute path, or a path relative to the current directory]
14
+ This sort of "lite" mode comes with a few minor limitations:
15
+ * Snippets &[only_defined_through] %>[snippet:].
16
+ * Project configuration settings &[only_defined_through] %>[config:].
17
+ * Custom macros &[only_defined_through] %>[macro:].
18
+ * Images &[referenced_with_path], and will not be copied anywhere when the output file is generated.
19
+ * Stylesheets &[referenced_with_path], or the name of an existing Glyph =>[#default_stylesheets|system stylesheet].
20
+ * The files included through the %>[include] &[referenced_with_path].
21
+ ]
22
+ ]
23
+ ]
@@ -0,0 +1,77 @@
1
+ section[
2
+ @title[Using Glyph programmatically]
3
+ txt[
4
+ Besides using Glyph from the command line, you can also use it straight from your code. Glyph's public =>[http://yardoc.org/docs/glyph/Glyph|API] is simple and can be used to:
5
+ * Retrieve and update configuration settings (using @Glyph\[\]@ and @Glyph\[\]=@)
6
+ * Filter text to HTML (using @Glyph#filter@)
7
+ * Compile Glyph source files into HTML or PDF files (using @Glyph#compile@)
8
+
9
+ That's pretty much it. Of course, both the @filter@ and @compile@ method cause Glyph to run in =>[#lite_mode|_lite_ mode], so the same =>[#lite_limitations|limitations] apply.
10
+ ]
11
+ tip[
12
+ txt[
13
+ For an example on how to use Glyph programmatically (specifically in conjunction with the =>[http://nanoc.stoneship.org/|nanoc] static site generator), see =>[http://github.com/h3rald/h3rald|h3rald.com source code], in particular:
14
+ * =>[http://github.com/h3rald/h3rald/blob/master/lib/glyph-data.rb|lib/glyph-data.rb] -- updating configuration settings.
15
+ * =>[http://github.com/h3rald/h3rald/blob/master/lib/glyph-filter.rb|lib/glyph-data.rb] -- using the @Glyph#filter@ method.
16
+ * =>[http://github.com/h3rald/h3rald/blob/master/Rules|Rules] -- using the @Glyph#compile@ method to generate PDF files.
17
+ ]
18
+ ]
19
+ section[
20
+ @title[Modes]
21
+ @id[modes]
22
+ p[It is possible to specify some flags (or "modes") to make Glyph behave slightly different than normal, as shown in the following table (by default, none of these is used).]
23
+ table[
24
+ tr[
25
+ th[Name]
26
+ th[Writer Method]
27
+ th[Reader Method]
28
+ th[Description]
29
+ ]
30
+ tr[
31
+ td[Test Mode]
32
+ td[code[Glyph.test_mode=]]
33
+ td[code[Glyph.test?]]
34
+ td[Used internally by the code[rake spec] task to run Glyph's specs.]
35
+ ]
36
+ tr[
37
+ td[Library Mode]
38
+ td[code[Glyph.library_mode=]]
39
+ td[code[Glyph.library?]]
40
+ td[If enabled, the #>[compile] command will raise exceptions instead of printing errors on the screen. Enabled by the code[Glyph.compile] command.]
41
+ ]
42
+ tr[
43
+ td[Debug Mode]
44
+ td[code[Glyph.debug_mode=]]
45
+ td[code[Glyph.debug?]]
46
+ td[If enabled, additional diagnostic information (such as backtraces or macro values) will be displayed. Enabled by specifying the =>[#debug_switch|debug switch] when running a Glyph command.]
47
+ ]
48
+ tr[
49
+ td[Lite Mode]
50
+ td[code[Glyph.lite_mode=]]
51
+ td[code[Glyph.lite?]]
52
+ td[
53
+ txt[
54
+ Used to compile =>[#lite_mode|single files]. Enabled by:
55
+ * The @Glyph.compile@ and @Glyph.filter@ methods.
56
+ * The #>[compile], if at least one parameter is supplied.
57
+ ]
58
+ ]
59
+ ]
60
+ tr[
61
+ td[Safe Mode]
62
+ td[code[Glyph.safe_mode=]]
63
+ td[code[Glyph.safe?]]
64
+ td[
65
+ txt[
66
+ If enabled, the following macros cannot be used and will return an error:
67
+ * %>[ruby]
68
+ * %>[macro:]
69
+ * %>[include]
70
+ * %>[rewrite:]
71
+ * %>[config:]
72
+ ]
73
+ ]
74
+ ]
75
+ ]
76
+ ]
77
+ ]
@@ -0,0 +1,21 @@
1
+ section[
2
+ @title[Bookmarks and Headers]
3
+ txt[
4
+ The =>[&[yardoc]/Macro|@Glyph::Macro@] class also includes a few methods to check and store bookmarks and headers. Consider for example the following source code for the %>[anchor]:
5
+ ]
6
+
7
+ highlight[=ruby|
8
+ macro :anchor do
9
+ ident, title = @params
10
+ macro_error "Bookmark '#{ident}' already exists" if bookmark? ident
11
+ bookmark :id => ident, :title => title
12
+ %{<a id="#{ident}">#{title}</a>}
13
+ end
14
+ =]
15
+
16
+ txt[
17
+ The @bookmark?@ method can be used to check the existance of a particular ID within the whole document, while the @bookmark@ method is used to store bookmark IDs and titles. In a similar way, you can use @header?@ and @header@ methods to check the existance of headers within the documents or store new ones.
18
+ ]
19
+ ]
20
+
21
+
@@ -0,0 +1,13 @@
1
+ section[
2
+ @title[Further Reading]
3
+ txt[
4
+ For more examples on how to create more complex macros, have a look at the =>[http://github.com/h3rald/glyph/tree/master/macros/|source code] of the existing ones.
5
+
6
+ To gain a deeper understanding on how macros are executed, have a look at the following Glyph classes:
7
+ * class[Glyph::Parser]
8
+ * class[Glyph::SyntaxNode]
9
+ * class[Glyph::Interpreter]
10
+ * class[Glyph::Document]
11
+ * class[Glyph::Macro]
12
+ ]
13
+ ]
@@ -0,0 +1,79 @@
1
+ section[
2
+ @title[A quick look at Glyph's internals]
3
+ p[If you plan on extending Glyph, knowing how it works inside helps. It is not mandatory by any means, but it definitely helps, especially when creating complex macros.]
4
+ p[What happens behind the scenes when you call code[glyph compile]? Glyph's code is parsed, analyzed and then translated into text, and here's how:]
5
+ figure[document_generation.png|A sequence diagram for document generation]
6
+ txt[From the diagram, it is possible to divide the document generation process into three phases:
7
+ * The _Parsing Phase_ starts when a chunk of Glyph code is passed (by the code[generate:document] Rake task, for example) to a class[Glyph::Interpreter]. The interpreter initializes a class[Glyph::Parser] that parses the code and returns an _Abstract Syntax Tree_ (AST) of class[Glyph::SyntaxNode] objects.
8
+ * The _Analysis Phase_ (Processing) starts when the interpreter method calls the @analyze@ method, instantiating a new class[Glyph::Document]. The @Glyph::Document@ object evaluates the AST expanding all macro nodesth (that's when macros are executed) and generates string.
9
+ * The _Finalization Phase_ (Post-Processing) starts when the interpreter calls the @finalyze@ method, causing the @Glyph::Document@ object to perform a series of finalizations on the string obtained after analysis, i.e. it replaces escape sequences and placeholders.
10
+ ]
11
+ section[
12
+ @title[Example: A short note]
13
+ p[As an example, consider the following Glyph code:]
14
+ highlight[=html|
15
+ fmi[something\|#test]
16
+ ...
17
+ section[
18
+ @title[Test Section]
19
+ @id[test]
20
+ ...
21
+ ]
22
+ =]
23
+ p[This simple snippet uses the %>[fmi] to link to a section later on in the document. When parsed, the produced AST is the following:]
24
+ highlight[=ruby|
25
+ {:name=>:"--"}
26
+ {:name=>:fmi, :escape=>false}
27
+ {:name=>:"0"}
28
+ {:value=>"something"}
29
+ {:name=>:"1"}
30
+ {:value=>"#test"}
31
+ {:value=>"\\n"}
32
+ {:value=>"\\\\[", :escaped=>true}
33
+ {:value=>"..."}
34
+ {:value=>"\\\\]", :escaped=>true}
35
+ {:value=>"\\n"}
36
+ {:name=>:section, :escape=>false}
37
+ {:name=>:"0"}
38
+ {:value=>"\\n\\t"}
39
+ {:value=>"\\n\\t"}
40
+ {:value=>"\\n"}
41
+ {:value=>"\\\\[", :escaped=>true}
42
+ {:value=>"..."}
43
+ {:value=>"\\\\]", :escaped=>true}
44
+ {:value=>"\\\n"}
45
+ {:name=>:title, :escape=>false}
46
+ {:value=>"Test Section"}
47
+ {:name=>:id, :escape=>false}
48
+ {:value=>"test"}
49
+ =]
50
+ p[This output is produced by calling the code[inspect] method on the AST. Each class[Glyph::SyntaxNode] object in the tree is basically an ordinary Glyph Hash with a parent and 0 or more chidren, so the code snippets above shows how the syntax nodes are nested.]
51
+ p[The AST contains information about macro, parameter and attribute names, and escaping, and raw text values (the nodes without a code[:name] key), but nothing more.]
52
+ p[When the AST is analyzed, the resulting textual output is the following:]
53
+ highlight[=html|
54
+ <span class="fmi">for more information on something, see ‡‡‡‡‡PLACEHOLDER ¤ 1‡‡‡‡‡
55
+ </span>
56
+ \\\\\.[...\\\\\.]
57
+ <div class="section">
58
+ <h2 id="test">Test Section</h2>
59
+ \\\\\.[...\\\\\.]
60
+
61
+ </div>
62
+ =]
63
+ p[This looks almost perfect, except that:]
64
+ ul[
65
+ li[There's a nasty placeholder instead of a link: this is due to the fact that when the link is processed, there is no code[#text] anchor in the document, but there may be one afterwards (and there will be).]
66
+ li[There are some escaped brackets.]
67
+ ]
68
+ p[Finally, when the document is finalized, placeholders and escape sequences are removed and the final result is the following:]
69
+ highlight[=html|
70
+ <span class="fmi">for more information on something, see <a href="#test">Test Section</a></span>
71
+ [...]
72
+ <div class="section">
73
+ <h2 id="test">Test Section</h2>
74
+ [...]
75
+
76
+ </div>
77
+ =]
78
+ ]
79
+ ]
@@ -0,0 +1,51 @@
1
+ section[
2
+ @title[Interpreting Glyph Code]
3
+ @id[interpreting]
4
+
5
+ txt[What if you need to evaluate some Glyph code _within_ a macro? Say for example you want to transform a parameter in a link, and you want to make sure that link gets validated exactly like the others, in this case, you can use the @interpret@ method, as follows:]
6
+
7
+ highlight[=ruby|
8
+ macro :fmi do
9
+ topic, href = @params
10
+ link = placeholder do \|document\|
11
+ interpret "link[#{href}]"
12
+ end
13
+ %{<span class="fmi">for more information on #{topic}, see #{link}</span>}
14
+ end
15
+ =]
16
+
17
+ txt[
18
+ When the @interpret@ method is called, the following happens:
19
+ # A new Glyph document is created from the @String@ passed to the method.
20
+ # The bookmarks, headers and placeholders are passed from the main document to the new one. Because they are stored in arrays and hashes, they are passed by reference, so for example any new bookmark stored in the new document will also become available in the main document.
21
+ # Any macro included in the @String@ is evaluated, and the resulting text is returned by the method. Note that this new document does not get finalized: in other words, placeholders will be left as they are, and they'll eventually be replaced when _the main document_ is finalized.
22
+ ]
23
+
24
+ section[
25
+ @title[Rewriting]
26
+ @id[rewriting]
27
+ p[While the code[interpret] method is useful to evaluate Glyph code in a macro while performing other actions (storing a bookmark, checking for the presence of an anchor, etc.), in some cases it may not be necessary. If you simply want your macro to be converted into existing Glyph macro without performing any action excepting parameter substitution, you can just use the %>[rewrite:] within youy Glyph document]
28
+ p[Consider the following macro definition:]
29
+ highlight[=ruby|
30
+ macro :issue do
31
+ interpret %{
32
+ tr[
33
+ td[\.=>[http://github.com/h3rald/glyph/issues/closed#issue/#{param[0]}\|##{param(0)}]]
34
+ td[txt[#{param(1)}]]
35
+ ]
36
+ }
37
+ end
38
+ =]
39
+ p[The code[issue] macro is only rewriting existing Glyph code around the two parameters provided. In this case, it is possible to do exactly the same thing using the %>[rewrite:]:]
40
+ highlight[=html|
41
+ rewrite:[issue\|
42
+ tr[
43
+ td[\.=>[http://github.com/h3rald/glyph/issues/closed#issue/{{0}}\|#{{0}}]]
44
+ td[txt[{{1}}]]
45
+ ]
46
+ ]
47
+ =]
48
+ p[Within the %>[rewrite:], it is possible to use a special syntax to call the code[raw_attr] or code[raw_param] methods: br[]
49
+ code[{{]em[parameter_number] or em[attribute_name]code[}}]]
50
+ ]
51
+ ]