plugin-coding-conventions.dita 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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>plug-ins
  16. <indexterm>best practices</indexterm></indexterm>
  17. <indexterm>upgrading
  18. <indexterm>best practices</indexterm></indexterm>
  19. <indexterm>XSLT
  20. <indexterm>best practices</indexterm></indexterm>
  21. <indexterm>preprocessing
  22. <indexterm>XSLT</indexterm></indexterm>
  23. </keywords>
  24. </metadata>
  25. </prolog>
  26. <conbody>
  27. <section>
  28. <title>Best practices</title>
  29. <p>Adhering to certain development practices will properly isolate your code from that of DITA Open Toolkit. This
  30. will make it easier to you to upgrade to new versions of DITA-OT when they are released.</p>
  31. <ul>
  32. <li>
  33. <p>Use a properly-constructed DITA-OT plug-in.</p></li>
  34. <li>
  35. <p>Use a version control system to store your code.</p></li>
  36. <li>
  37. <p>Store the source code of your plug-ins outside of the DITA-OT installation directory, and add the
  38. repository location to the list of plug-in directories defined in the <parmname>plugindirs</parmname> entry
  39. of the
  40. <xref keyref="configuration-properties-file"><filepath>configuration.properties</filepath> file</xref>.
  41. </p></li>
  42. <li>
  43. <p>Never modify any of the core DITA-OT code.</p>
  44. <note type="tip">You may want to set the permissions on default plug-in directories such as
  45. <filepath>org.dita.pdf2</filepath> to “read-only” to ensure that you do not accidentally modify the files
  46. within as you develop your customized plug-in.</note></li>
  47. <li>
  48. <p>Avoid copying entire DITA-OT files into your customization plug-in. When you only copy the attribute sets
  49. and templates that you need to override, there is less risk of impact from new features or fixes in the base
  50. code, making your code more stable and easier to upgrade between releases.</p></li>
  51. <li>
  52. <p>If you only need to change a few attribute sets and templates, you may prefer to store your overrides in
  53. <filepath>custom.xsl</filepath> files, or a simple folder hierarchy within your custom plug-in.</p></li>
  54. <li>
  55. <p>In cases that require substantial customizations, you may prefer to organize the files in a folder
  56. structure that mimics the hierarchy of the default plug-in you are customizing. This facilitates comparisons
  57. with the default settings in the base plug-in and makes it easier to migrate your changes to new toolkit
  58. versions. See
  59. <xref keyref="pdf-plugin-structure"/> for information on the files in the base PDF plug-in.</p></li>
  60. <li>
  61. <p>Upgrade your customization plug-in to new versions of DITA-OT regularly. Do not wait through several major
  62. releases before upgrading.</p></li>
  63. </ul></section>
  64. <section>
  65. <title>Use a custom namespace</title>
  66. <p>For XSLT customizations, use a custom namespace for any modified template modes, template names, attribute
  67. sets, functions, and variables. This helps to clarify which portions of the code are specific to your
  68. customizations, and serves to isolate your changes in the event that items with the same name are added to the
  69. base plug-ins in the future.</p>
  70. <p>For example, instead of creating a template named <codeph>searchbar</codeph>, use something like
  71. <codeph>corp:searchbar</codeph> instead. This ensures that if future versions of DITA-OT add a
  72. <codeph>searchbar</codeph> template, your custom version will be unaffected.</p>
  73. <p>Instead of: <codeblock outputclass="language-xml">&lt;xsl:template name="searchbar"/></codeblock></p>
  74. <p>use: <codeblock outputclass="language-xml">&lt;xsl:template name="corp:searchbar"/></codeblock></p>
  75. </section>
  76. <section conkeyref="reusable-components/use-xslt2"/>
  77. <section>
  78. <title>Use custom <xmlelement>pipeline</xmlelement> elements</title>
  79. <indexterm>Ant
  80. <indexterm><xmlelement>pipeline</xmlelement></indexterm></indexterm>
  81. <indexterm>Ant
  82. <indexterm><xmlelement>xslt</xmlelement></indexterm></indexterm>
  83. <indexterm>Ant
  84. <indexterm><xmlelement>style</xmlelement></indexterm></indexterm>
  85. <p>In Ant scripts, use the XSLT module from DITA-OT instead of Ant’s built-in <xmlelement>xslt</xmlelement> or
  86. <xmlelement>style</xmlelement> tasks. </p>
  87. <p>The XSLT module allows access to DITA-OT features like using the job configuration to select files in the
  88. temporary folder based on file metadata and custom XSLT extension functions. </p>
  89. <p>Instead of:</p>
  90. <p><codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace">&lt;xslt style="${dita.plugin.example.dir}/custom.xsl"
  91. basedir="${dita.temp.dir}"
  92. destdir="${dita.output.dir}"
  93. includesfile="${dita.temp.dir}/${fullditatopicfile}"/></codeblock></p>
  94. <p>use:</p>
  95. <p><codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace">&lt;pipeline>
  96. &lt;xslt style="${dita.plugin.example.dir}/custom.xsl"
  97. destdir="${dita.output.dir}">
  98. &lt;ditafileset format="dita" />
  99. &lt;/xslt>
  100. &lt;/pipeline></codeblock></p>
  101. </section>
  102. <section>
  103. <title>Use the plug-in directory property</title>
  104. <p>In Ant scripts, always refer to files in other plug-ins using the
  105. <codeph>dita.plugin.<varname>plugin-id</varname>.dir</codeph> property.</p>
  106. <p>Instead of:</p>
  107. <p><codeblock outputclass="language-xml">&lt;property name="base" location="../example/custom.xsl"/></codeblock></p>
  108. <p>use:</p>
  109. <p><codeblock outputclass="language-xml">&lt;property name="base" location="${dita.plugin.example.dir}/custom.xsl"/></codeblock></p>
  110. <p>This fixes cases where plug-ins are installed to custom plug-in directories or the plug-in folder name doesn’t
  111. match the plug-in ID.</p>
  112. <note type="tip">For details, see
  113. <xref keyref="referencing-other-plugins"/>.</note>
  114. </section>
  115. <section>
  116. <title>Use the <codeph>plugin</codeph> URI scheme</title>
  117. <indexterm>Ant
  118. <indexterm><xmlelement>xsl:import</xmlelement></indexterm></indexterm>
  119. <indexterm>Ant
  120. <indexterm><xmlelement>xsl:include</xmlelement></indexterm></indexterm>
  121. <p>In XSLT, use the <codeph>plugin</codeph> URI scheme in <xmlelement>xsl:import</xmlelement> and
  122. <xmlelement>xsl:include</xmlelement> to reference files in other plug-ins.</p>
  123. <p>Instead of:</p>
  124. <p><codeblock outputclass="language-xml">&lt;xsl:import href="../../org.dita.base/xsl/common/output-message.xsl"/></codeblock></p>
  125. <p>use:</p>
  126. <p><codeblock outputclass="language-xml">&lt;xsl:import href="plugin:org.dita.base:xsl/common/output-message.xsl"/></codeblock></p>
  127. <p>As with the plug-in directory property in Ant, this allows plug-ins to resolve to the correct directory even
  128. when a plug-in moves to a new location. The plug-in is referenced using the syntax
  129. <codeph>plugin:<varname>plugin-id</varname>:<varname>path/within/plugin/file.xsl</varname></codeph>.</p>
  130. <note type="tip">For details, see
  131. <xref keyref="referencing-other-plugins"/>.</note>
  132. </section>
  133. <section>
  134. <title>Use <xmlelement>ditafileset</xmlelement> to select files</title>
  135. <indexterm>Ant
  136. <indexterm><xmlelement>ditafileset</xmlelement></indexterm></indexterm>
  137. <indexterm><xmlelement>ditafileset</xmlelement></indexterm>
  138. <indexterm>images
  139. <indexterm>selecting</indexterm></indexterm>
  140. <indexterm>images
  141. <index-see-also>copy-files</index-see-also></indexterm>
  142. <p>In Ant scripts, use <xmlelement>ditafileset</xmlelement> to select resources in the temporary directory.</p>
  143. <p>For example, to select all images referenced by input DITA files, instead of:</p>
  144. <p><codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace">&lt;copy todir="${copy-image.todir}">
  145. &lt;fileset dir="${user.input.dir}">
  146. &lt;includes name="*.jpg"/>
  147. &lt;includes name="*.jpeg"/>
  148. &lt;includes name="*.png"/>
  149. &lt;includes name="*.gif"/>
  150. &lt;includes name="*.svg"/>
  151. &lt;/fileset>
  152. &lt;/copy></codeblock></p>
  153. <p>use:</p>
  154. <p><codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace">&lt;copy todir="${copy-image.todir}">
  155. &lt;ditafileset format="image" />
  156. &lt;/copy></codeblock>
  157. </p>
  158. <p>The <xmlelement>ditafileset</xmlelement> resource collection can be used to select different types of
  159. files.</p>
  160. <table outputclass="table-hover" frame="none" colsep="0" rowsep="1">
  161. <title>Usage examples of <xmlelement>ditafileset</xmlelement></title>
  162. <tgroup cols="2">
  163. <colspec colwidth="1*"/>
  164. <colspec colwidth="1*"/>
  165. <thead>
  166. <row>
  167. <entry>Example</entry>
  168. <entry>Description</entry>
  169. </row>
  170. </thead>
  171. <tbody>
  172. <row>
  173. <entry><codeph>&lt;ditafileset format="dita"/></codeph></entry>
  174. <entry>Selects all DITA topics in the temporary directory.</entry>
  175. </row>
  176. <row>
  177. <entry><codeph>&lt;ditafileset format="ditamap"/></codeph></entry>
  178. <entry>Selects all DITA maps in the temporary directory.</entry>
  179. </row>
  180. <row>
  181. <entry><codeph>&lt;ditafileset format="image"/></codeph></entry>
  182. <entry>Selects images of all known types in the temporary directory.</entry>
  183. </row>
  184. </tbody>
  185. </tgroup>
  186. </table>
  187. </section>
  188. <section conkeyref="reusable-components/validating-plugins"/>
  189. </conbody>
  190. </concept>