extended-functionality.dita 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "reference.dtd">
  3. <!-- This file is part of the DITA Open Toolkit project. See the accompanying LICENSE file for applicable license. -->
  4. <reference id="code-reference">
  5. <title>Extended code reference processing</title>
  6. <titlealts>
  7. <navtitle>Coderef extensions</navtitle>
  8. </titlealts>
  9. <shortdesc>DITA-OT provides additional code reference processing support beyond that which is mandated by the DITA
  10. specification. These extensions can be used to define character encodings or line ranges for use in code
  11. blocks.</shortdesc>
  12. <refbody>
  13. <section id="coderef-charset">
  14. <title>Character set definition</title>
  15. <p>DITA-OT supports defining the code reference target file encoding using the <xmlatt>format</xmlatt> attribute.
  16. The supported format is:</p>
  17. <codeblock>format (";" space* "charset=" charset)?</codeblock>
  18. <p>If a character set is not defined, the system default character set will be used. If the character set is not
  19. recognized or supported, the <msgnum>DOTJ052E</msgnum> error is thrown and the system default character set is
  20. used as a fall-back.</p>
  21. </section>
  22. <example>
  23. <codeblock>&lt;coderef href="unicode.txt" format="txt; charset=UTF-8"/></codeblock>
  24. </example>
  25. <section>
  26. <title>Line range extraction</title>
  27. <p>Code references can be limited to extract only a specified line range by defining the
  28. <codeph>line-range</codeph> pointer in the URI fragment. The format is:</p>
  29. <codeblock>uri ("#line-range(" start ("," end)? ")" )?</codeblock>
  30. <p>Start and end line numbers start from 1 and are inclusive. If the end range is omitted, the range ends on the
  31. last line of the file.</p>
  32. </section>
  33. <example>
  34. <codeblock>&lt;coderef href="Parser.scala#line-range(5, 10)" format="scala"/></codeblock>
  35. <p>Only lines from 5 to 10 will be included in the output.</p>
  36. </example>
  37. <section>
  38. <title>RFC 5147</title>
  39. <p>DITA-OT also supports the line position and range syntax from
  40. <xref keyref="rfc5147"/>. The format for line range is:</p>
  41. <codeblock>uri ("#line=" start? "," end? )?</codeblock>
  42. <p>Start and end line numbers start from 0 and are inclusive and exclusive, respectively. If the start range is
  43. omitted, the range starts from the first line; if the end range is omitted, the range ends on the last line of
  44. the file. The format for line position is:</p>
  45. <codeblock>uri ("#line=" position )?</codeblock>
  46. <p>Position line number starts from 0.</p>
  47. </section>
  48. <example>
  49. <codeblock>&lt;coderef href="Parser.scala#line=4,10" format="scala"/></codeblock>
  50. <p>Only lines from 5 to 10 will be included in the output.</p>
  51. </example>
  52. <section>
  53. <title>Line range by content</title>
  54. <p>Instead of specifying line numbers, you can also select lines to include in the code reference by specifying
  55. keywords (or “<term>tokens</term>”) that appear in the referenced file.</p>
  56. <div id="coderef-by-content">
  57. <p>DITA-OT supports the <codeph>token</codeph> pointer in the URI fragment to extract a line range based on the
  58. file content. The format for referencing a range of lines by content is:</p>
  59. <codeblock>uri ("#token=" start? ("," end)? )?</codeblock>
  60. <p>Lines identified using start and end tokens are exclusive: the lines that contain the start token and end
  61. token will be not be included. If the start token is omitted, the range starts from the first line in the
  62. file; if the end token is omitted, the range ends on the last line of the file. </p>
  63. </div>
  64. </section>
  65. <example>
  66. <p>Given a Haskell source file named <filepath>fact.hs</filepath> with the following content,</p>
  67. <codeblock><coderef href="../resources/fact.hs"/></codeblock>
  68. <p>a range of lines can be referenced as:</p>
  69. <codeblock>&lt;coderef href="fact.hs#token=START-FACT,END-FACT"/></codeblock>
  70. <p>to include the range of lines that follows the <codeph>START-FACT</codeph> token, up to (but not including) the
  71. line that contains the <codeph>END-FACT</codeph> token. The resulting <xmlelement>codeblock</xmlelement> would
  72. contain the following lines:</p>
  73. <codeblock><coderef href="../resources/fact.hs#token=START-FACT,END-FACT"/></codeblock>
  74. <note type="tip" id="coderef-by-content-tip">This approach can be used to reference code samples that are
  75. frequently edited. In these cases, referencing line ranges by line number can be error-prone, as the target line
  76. range for the reference may shift if preceding lines are added or removed. Specifying ranges by line content
  77. makes references more robust, as long as the <codeph>token</codeph> keywords are preserved when the referenced
  78. resource is modified.</note></example>
  79. </refbody>
  80. </reference>