html-customization-plugin-javascript.dita 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE task PUBLIC "-//OASIS//DTD DITA Task//EN" "task.dtd">
  3. <!-- This file is part of the DITA Open Toolkit project. See the accompanying LICENSE file for applicable license. -->
  4. <task id="custom-html-plugin-webfont">
  5. <title>Inserting JavaScript in generated HTML</title>
  6. <titlealts>
  7. <navtitle>Inserting JavaScript</navtitle>
  8. </titlealts>
  9. <shortdesc>JavaScript code can be bundled in a custom plug-in and automatically inserted into the generated HTML pages
  10. to enable web analytics or dynamic content delivery.</shortdesc>
  11. <prolog>
  12. <metadata>
  13. <keywords>
  14. <indexterm><xmlelement>footer</xmlelement></indexterm>
  15. <indexterm><xmlelement>require</xmlelement></indexterm>
  16. <indexterm><xmlelement>head</xmlelement></indexterm>
  17. <indexterm>transformations<indexterm end="html"/></indexterm> <!-- LE: This range is not appearing in the FOP index. -->
  18. <indexterm>HTML5<indexterm>JavaScript, adding</indexterm></indexterm>
  19. </keywords>
  20. </metadata>
  21. </prolog>
  22. <taskbody>
  23. <context>
  24. <p>This scenario walks through the process of creating a very simple plug-in
  25. (<codeph>com.example.html5-javascript</codeph>) that creates a new transformation type:
  26. <option>html5-javascript</option>. </p>
  27. <p>The <option>html5-javascript</option> transformation includes a custom page footer file with a JavaScript
  28. tracking snippet and sets the <parmname>args.ftr</parmname> parameter to integrate the script content in the
  29. HTML5 <xmlelement>footer</xmlelement> element of the generated pages.</p>
  30. <note>This example inserts a tracking snippet for Google Analytics, but the basic approach is the same for other
  31. analytics platforms or similar use cases that require custom JavaScript.</note>
  32. </context>
  33. <steps>
  34. <step>
  35. <cmd>In the <filepath>plugins</filepath> directory, create a directory named
  36. <filepath>com.example.html5-javascript</filepath>.</cmd>
  37. </step>
  38. <step>
  39. <cmd>In the new <filepath>com.example.html5-javascript</filepath> directory, create a plug-in configuration file
  40. (<filepath>plugin.xml</filepath>) that declares the new <option>html5-javascript</option> transformation and
  41. its dependencies.</cmd>
  42. <stepxmp>
  43. <fig>
  44. <title>Sample <filepath>plugin.xml</filepath> file</title>
  45. <codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace"><coderef href="../samples/plugins/com.example.html5-javascript/plugin.xml"/></codeblock>
  46. </fig>
  47. <note>This plug-in will extend the default HTML5 transformation, so the <xmlelement>require</xmlelement>
  48. element explicitly defines <filepath>org.dita.html5</filepath> as a dependency.</note>
  49. </stepxmp>
  50. </step>
  51. <step>
  52. <cmd>In the <filepath>com.example.html5-javascript</filepath> directory, create a subdirectory named
  53. <filepath>include</filepath>.</cmd>
  54. </step>
  55. <step>
  56. <cmd>In the new <filepath>include</filepath> subdirectory, create a file named
  57. <filepath>javascript.ftr.xml</filepath> with your custom JavaScript code.</cmd>
  58. <stepxmp>
  59. <fig>
  60. <title>Sample <filepath>javascript.ftr.xml</filepath> file</title>
  61. <codeblock outputclass="language-javascript normalize-space show-line-numbers show-whitespace"><coderef href="../samples/plugins/com.example.html5-javascript/include/javascript.ftr.xml"/></codeblock>
  62. </fig>
  63. <p>The division wrapper will be discarded when generating HTML files, and the contents will be inserted into
  64. the <xmlelement>footer</xmlelement> element of each page.</p>
  65. </stepxmp>
  66. </step>
  67. <step>
  68. <cmd>In the <filepath>com.example.html5-javascript</filepath> root directory, add an Ant script
  69. (<filepath>build_html5-javascript.xml</filepath>) to define the transformation type and set the path to the
  70. JavaScript footer file created in the previous step.</cmd>
  71. <stepxmp>
  72. <fig>
  73. <title>Sample build file: <filepath>build_html5-javascript.xml</filepath></title>
  74. <codeblock outputclass="language-xml normalize-space show-line-numbers show-whitespace"><coderef href="../samples/plugins/com.example.html5-javascript/build_html5-javascript.xml"/></codeblock>
  75. </fig>
  76. </stepxmp>
  77. </step>
  78. </steps>
  79. <result>
  80. <note type="tip">The files for this sample plug-in are included in the DITA-OT installation directory under
  81. <filepath>docsrc/samples/plugins/com.example.html5-javascript/</filepath> and on <xref
  82. href="https://github.com/dita-ot/docs/tree/develop/samples/plugins/com.example.html5-javascript" format="html"
  83. scope="external">GitHub</xref>.</note>
  84. <p>The plug-in directory has the following layout and files:</p>
  85. <codeblock>com.example.html5-javascript
  86. ├── build_html5-javascript.xml
  87. ├── include
  88. │   └── javascript.ftr.xml
  89. └── plugin.xml</codeblock>
  90. </result>
  91. <postreq>
  92. <ol>
  93. <li>Run <filepath conref="../resources/conref-task.dita#ID/dita-cmd"/>
  94. <parmname>--install</parmname> to install the plug-in and make the <option>html5-javascript</option>
  95. transformation available.</li>
  96. <li>Build output with the new transformation type to verify that the plug-in works as intended.
  97. <codeblock><filepath conref="../resources/conref-task.dita#ID/dita-cmd"/> <parmname>--input</parmname>=<varname>my.ditamap</varname> <parmname>--format</parmname>=<option>html5-javascript</option></codeblock></li>
  98. <li>Open one of the generated HTML topic files in a modern web browser and check the JavaScript
  99. <uicontrol>Console</uicontrol>. When the page is loaded, <msgph>Adding Google Analytics tracker</msgph> will
  100. appear on the console to verify that the sample script is loaded.</li>
  101. <li>Remove the <codeph>console.log</codeph> debugging message from the sample JavaScript code, and replace the
  102. <codeph>'UA-XXXXX-Y'</codeph> placeholder string with the tracking ID of the Google Analytics property you
  103. wish to track.</li>
  104. </ol>
  105. <note type="tip">This example places the JavaScript code in the page footer to ensure that page display is not
  106. delayed while the script is loaded. If your JavaScript code supports pre-loading and your application targets
  107. modern browsers that recognize the <codeph>async</codeph> script attribute, you may prefer to insert the
  108. JavaScript snippet in the <xmlelement>head</xmlelement> element of the generated HTML files using the
  109. <parmname>args.hdf</parmname> parameter intead.</note>
  110. </postreq>
  111. </taskbody>
  112. </task>