codewalk.xml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <codewalk title="How to Write a Codewalk">
  2. <step title="Introduction" src="doc/codewalk/codewalk.xml">
  3. A codewalk is a guided tour through a piece of code.
  4. It consists of a sequence of steps, each typically explaining
  5. a highlighted section of code.
  6. <br/><br/>
  7. The <a href="/cmd/godoc">godoc</a> web server translates
  8. an XML file like the one in the main window pane into the HTML
  9. page that you're viewing now.
  10. <br/><br/>
  11. The codewalk with URL path <code>/doc/codewalk/</code><i>name</i>
  12. is loaded from the input file <code>$GOROOT/doc/codewalk/</code><i>name</i><code>.xml</code>.
  13. <br/><br/>
  14. This codewalk explains how to write a codewalk by examining
  15. its own source code,
  16. <code><a href="/doc/codewalk/codewalk.xml">$GOROOT/doc/codewalk/codewalk.xml</a></code>,
  17. shown in the main window pane to the left.
  18. </step>
  19. <step title="Title" src="doc/codewalk/codewalk.xml:/title=/">
  20. The codewalk input file is an XML file containing a single
  21. <code>&lt;codewalk&gt;</code> element.
  22. That element's <code>title</code> attribute gives the title
  23. that is used both on the codewalk page and in the codewalk list.
  24. </step>
  25. <step title="Steps" src="doc/codewalk/codewalk.xml:/&lt;step/,/step&gt;/">
  26. Each step in the codewalk is a <code>&lt;step&gt;</code> element
  27. nested inside the main <code>&lt;codewalk&gt;</code>.
  28. The step element's <code>title</code> attribute gives the step's title,
  29. which is shown in a shaded bar above the main step text.
  30. The element's <code>src</code> attribute specifies the source
  31. code to show in the main window pane and, optionally, a range of
  32. lines to highlight.
  33. <br/><br/>
  34. The first step in this codewalk does not highlight any lines:
  35. its <code>src</code> is just a file name.
  36. </step>
  37. <step title="Specifying a source line" src='doc/codewalk/codewalk.xml:/title="Title"/'>
  38. The most complex part of the codewalk specification is
  39. saying what lines to highlight.
  40. Instead of ordinary line numbers,
  41. the codewalk uses an address syntax that makes it possible
  42. to describe the match by its content.
  43. As the file gets edited, this descriptive address has a better
  44. chance to continue to refer to the right section of the file.
  45. <br/><br/>
  46. To specify a source line, use a <code>src</code> attribute of the form
  47. <i>filename</i><code>:</code><i>address</i>,
  48. where <i>address</i> is an address in the syntax used by the text editors <i>sam</i> and <i>acme</i>.
  49. <br/><br/>
  50. The simplest address is a single regular expression.
  51. The highlighted line in the main window pane shows that the
  52. address for the &ldquo;Title&rdquo; step was <code>/title=/</code>,
  53. which matches the first instance of that <a href="/pkg/regexp">regular expression</a> (<code>title=</code>) in the file.
  54. </step>
  55. <step title="Specifying a source range" src='doc/codewalk/codewalk.xml:/title="Steps"/'>
  56. To highlight a range of source lines, the simplest address to use is
  57. a pair of regular expressions
  58. <code>/</code><i>regexp1</i><code>/,/</code><i>regexp2</i><code>/</code>.
  59. The highlight begins with the line containing the first match for <i>regexp1</i>
  60. and ends with the line containing the first match for <i>regexp2</i>
  61. after the end of the match for <i>regexp1</i>.
  62. Ignoring the HTML quoting,
  63. The line containing the first match for <i>regexp1</i> will be the first one highlighted,
  64. and the line containing the first match for <i>regexp2</i>.
  65. <br/><br/>
  66. The address <code>/&lt;step/,/step&gt;/</code> looks for the first instance of
  67. <code>&lt;step</code> in the file, and then starting after that point,
  68. looks for the first instance of <code>step&gt;</code>.
  69. (Click on the &ldquo;Steps&rdquo; step above to see the highlight in action.)
  70. Note that the <code>&lt;</code> and <code>&gt;</code> had to be written
  71. using XML escapes in order to be valid XML.
  72. </step>
  73. <step title="Advanced addressing" src="doc/codewalk/codewalk.xml:/Advanced/,/step&gt;/">
  74. The <code>/</code><i>regexp</i><code>/</code>
  75. and <code>/</code><i>regexp1</i><code>/,/</code><i>regexp2</i><code>/</code>
  76. forms suffice for most highlighting.
  77. <br/><br/>
  78. The full address syntax is summarized in this table
  79. (an excerpt of Table II from
  80. <a href="https://9p.io/sys/doc/sam/sam.html">The text editor <code>sam</code></a>):
  81. <br/><br/>
  82. <table>
  83. <tr><td colspan="2"><b>Simple addresses</b></td></tr>
  84. <tr><td><code>#</code><i>n</i></td>
  85. <td>The empty string after character <i>n</i></td></tr>
  86. <tr><td><i>n</i></td>
  87. <td>Line <i>n</i></td></tr>
  88. <tr><td><code>/</code><i>regexp</i><code>/</code></td>
  89. <td>The first following match of the regular expression</td></tr>
  90. <!-- not supported (yet?)
  91. <tr><td><code>–/</code><i>regexp</i><code>/</code></td>
  92. <td>The first previous match of the regular expression</td></tr>
  93. -->
  94. <tr><td><code>$</code></td>
  95. <td>The null string at the end of the file</td></tr>
  96. <tr><td colspan="2"><b>Compound addresses</b></td></tr>
  97. <tr><td><i>a1</i><code>+</code><i>a2</i></td>
  98. <td>The address <i>a2</i> evaluated starting at the right of <i>a1</i></td></tr>
  99. <tr><td><i>a1</i><code>-</code><i>a2</i></td>
  100. <td>The address <i>a2</i> evaluated in the reverse direction starting at the left of <i>a1</i></td></tr>
  101. <tr><td><i>a1</i><code>,</code><i>a2</i></td>
  102. <td>From the left of <i>a1</i> to the right of <i>a2</i> (default <code>0,$</code>).</td></tr>
  103. </table>
  104. </step>
  105. </codewalk>