plugin-coding-conventions.dita 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
  3. <!-- This file is part of the DITA Open Toolkit project. See the accompanying LICENSE file for applicable license. -->
  4. <concept id="ID">
  5. <title>Plug-in coding conventions</title>
  6. <titlealts>
  7. <navtitle>Coding conventions</navtitle>
  8. </titlealts>
  9. <shortdesc>To ensure custom plug-ins work well with the core toolkit code and remain compatible with future releases,
  10. the DITA Open Toolkit project recommends that plug-ins use modern development practices and common coding
  11. patterns.</shortdesc>
  12. <prolog>
  13. <metadata>
  14. <keywords>
  15. <indexterm end="plugins"/>
  16. <indexterm>plug-ins<indexterm>best practices</indexterm></indexterm>
  17. <indexterm>XSLT<indexterm>best practices</indexterm></indexterm>
  18. <indexterm>preprocessing<indexterm>XSLT</indexterm></indexterm>
  19. </keywords>
  20. </metadata>
  21. </prolog>
  22. <conbody>
  23. <section conkeyref="reusable-components/use-xslt2"/>
  24. <section>
  25. <title>Use custom <xmlelement>pipeline</xmlelement> elements</title>
  26. <div outputclass="div-index">
  27. <indexterm>Ant<indexterm><xmlelement>pipeline</xmlelement></indexterm></indexterm>
  28. <indexterm>Ant<indexterm><xmlelement>xslt</xmlelement></indexterm></indexterm>
  29. <indexterm>Ant<indexterm><xmlelement>style</xmlelement></indexterm></indexterm>
  30. </div>
  31. <p>In Ant scripts, use the XSLT module from DITA-OT instead of Ant’s built-in <xmlelement>xslt</xmlelement> or
  32. <xmlelement>style</xmlelement> tasks. </p>
  33. <p>The XSLT module allows access to DITA-OT features like using the job configuration to select files in the
  34. temporary folder based on file metadata and custom XSLT extension functions. </p>
  35. <p>Instead of:</p>
  36. <p><codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace">&lt;xslt style="${dita.plugin.example.dir}/custom.xsl"
  37. basedir="${dita.temp.dir}"
  38. destdir="${dita.output.dir}"
  39. includesfile="${dita.temp.dir}/${fullditatopicfile}"/></codeblock></p>
  40. <p>use:</p>
  41. <p><codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace">&lt;pipeline>
  42. &lt;xslt style="${dita.plugin.example.dir}/custom.xsl"
  43. destdir="${dita.output.dir}">
  44. &lt;ditafileset format="dita" />
  45. &lt;/xslt>
  46. &lt;/pipeline></codeblock></p>
  47. </section>
  48. <section>
  49. <title>Use the plug-in directory property</title>
  50. <p>In Ant scripts, always refer to files in other plug-ins using the
  51. <codeph>dita.plugin.<varname>plugin-id</varname>.dir</codeph> property.</p>
  52. <p>Instead of:</p>
  53. <p><codeblock outputclass="language-xml">&lt;property name="base" location="../example/custom.xsl"/></codeblock></p>
  54. <p>use:</p>
  55. <p><codeblock outputclass="language-xml">&lt;property name="base" location="${dita.plugin.example.dir}/custom.xsl"/></codeblock></p>
  56. <p>This fixes cases where plug-ins are installed to custom plug-in directories or the plug-in folder name doesn’t
  57. match the plug-in ID.</p>
  58. </section>
  59. <section>
  60. <title>Use <xmlelement>ditafileset</xmlelement> to select files</title>
  61. <div outputclass="div-index">
  62. <indexterm>Ant<indexterm><xmlelement>ditafileset</xmlelement></indexterm></indexterm>
  63. <indexterm><xmlelement>ditafileset</xmlelement></indexterm>
  64. <indexterm>images<indexterm>selecting</indexterm></indexterm>
  65. <indexterm>images<index-see-also>copy-files</index-see-also></indexterm>
  66. </div>
  67. <p>In Ant scripts, use <xmlelement>ditafileset</xmlelement> to select resources in the temporary directory.</p>
  68. <p>For example, to select all images referenced by input DITA files, instead of:</p>
  69. <p><codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace">&lt;copy todir="${copy-image.todir}">
  70. &lt;fileset dir="${user.input.dir}">
  71. &lt;includes name="*.jpg"/>
  72. &lt;includes name="*.jpeg"/>
  73. &lt;includes name="*.png"/>
  74. &lt;includes name="*.gif"/>
  75. &lt;includes name="*.svg"/>
  76. &lt;/fileset>
  77. &lt;/copy></codeblock></p>
  78. <p>use:</p>
  79. <p><codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace">&lt;copy todir="${copy-image.todir}">
  80. &lt;ditafileset format="image" />
  81. &lt;/copy></codeblock>
  82. </p>
  83. <p>The <xmlelement>ditafileset</xmlelement> resource collection can be used to select different types of
  84. files.</p>
  85. <table outputclass="table-hover" frame="none" colsep="0" rowsep="1">
  86. <title>Usage examples of <xmlelement>ditafileset</xmlelement></title>
  87. <tgroup cols="2">
  88. <colspec colwidth="1*"/>
  89. <colspec colwidth="1*"/>
  90. <thead>
  91. <row>
  92. <entry>Example</entry>
  93. <entry>Description</entry>
  94. </row>
  95. </thead>
  96. <tbody>
  97. <row>
  98. <entry><codeph>&lt;ditafileset format="dita"/></codeph></entry>
  99. <entry>Selects all DITA topics in the temporary directory.</entry>
  100. </row>
  101. <row>
  102. <entry><codeph>&lt;ditafileset format="ditamap"/></codeph></entry>
  103. <entry>Selects all DITA maps in the temporary directory.</entry>
  104. </row>
  105. <row>
  106. <entry><codeph>&lt;ditafileset format="image"/></codeph></entry>
  107. <entry>Selects images of all known types in the temporary directory.</entry>
  108. </row>
  109. </tbody>
  110. </tgroup>
  111. </table>
  112. </section>
  113. <section>
  114. <title>Use the <codeph>plugin</codeph> URI scheme</title>
  115. <div outputclass="div-index">
  116. <indexterm>Ant<indexterm><xmlelement>xsl:import</xmlelement></indexterm></indexterm>
  117. <indexterm>Ant<indexterm><xmlelement>xsl:include</xmlelement></indexterm></indexterm>
  118. </div>
  119. <p>In XSLT, use the <codeph>plugin</codeph> URI scheme in <xmlelement>xsl:import</xmlelement> and
  120. <xmlelement>xsl:include</xmlelement> to reference files in other plug-ins.</p>
  121. <p>Instead of:</p>
  122. <p><codeblock outputclass="language-xml">&lt;xsl:import href="../../org.dita.base/xsl/common/output-message.xsl"/></codeblock></p>
  123. <p>use:</p>
  124. <p><codeblock outputclass="language-xml">&lt;xsl:import href="plugin:org.dita.base:xsl/common/output-message.xsl"/></codeblock></p>
  125. <p>As with the plug-in directory property in Ant, this allows plug-ins to resolve to the correct directory even
  126. when a plug-in moves to a new location. The plug-in is referenced using the syntax
  127. <codeph>plugin:<varname>plugin-id</varname>:<varname>path/within/plugin/file.xsl</varname></codeph>.</p>
  128. </section>
  129. <section conkeyref="reusable-components/validating-plugins"/>
  130. </conbody>
  131. </concept>