CONTRIBUTING.adoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. = Contributing to Asciidoctor Spring Backends
  2. Unless otherwise noted, code in this project is released under the Apache 2.0 license.
  3. If you would like to contribute something, or want to hack on the code this document should help you get started.
  4. == Code of Conduct
  5. This project adheres to the Contributor Covenant link:CODE_OF_CONDUCT.adoc[code of conduct].
  6. By participating, you are expected to uphold this code. Please report unacceptable behavior to spring-code-of-conduct@pivotal.io.
  7. == Sign the Contributor License Agreement
  8. Before we accept a non-trivial patch or pull request we will need you to https://cla.pivotal.io/sign/spring[sign the Contributor License Agreement].
  9. Signing the contributor's agreement does not grant anyone commit rights to the main repository, but it does mean that we can accept your contributions, and you will get an author credit if we do.
  10. Active contributors might be asked to join the core team, and given the ability to merge pull requests.
  11. == Building from Source
  12. Source can be built from the command line using https://gradle.org[Gradle] on JDK 1.8 or above.
  13. We include https://docs.gradle.org/current/userguide/gradle_wrapper.html[Gradle's wrapper scripts] (`./gradlew` or `gradlew.bat`) that you can run rather than needing to install Gradle locally.
  14. The project can be built from the root directory using the standard Gradle command:
  15. [indent=0]
  16. ----
  17. $ ./gradlew build
  18. ----
  19. == Working with the Code
  20. There are quite a few moving parts to this project and a lot of different technologies involved.
  21. The project can broadly be split into two parts:
  22. . An AsciidoctorJ compatible extension providing the backends
  23. . A set of web assets (CSS & Javascript) used by the HTML generated by the backend.
  24. === AsciidoctorJ Backend
  25. The AsciidoctorJ backend is Ruby and Java code that provides a https://www.rubydoc.info/gems/asciidoctor/Asciidoctor/Converter[Asciidoctor::Converter] implementation.
  26. ==== Ruby Code
  27. The main converter implementation is written in Ruby.
  28. You can find the code in the link:src/main/ruby[`src/main/ruby`] folder.
  29. The code delegates to the standard `html5` converter, but changes a few areas to give us the HTML that we need.
  30. It also writes the CSS, Javascript and Images to the output directory.
  31. The code needs to be packaged as a Gem so that it can be picked up by AsciidoctorJ.
  32. This means that the Jar needs to have the following layout:
  33. ----
  34. specifications/
  35. spring-asciidoctor-backends-0.0.0.gemspec
  36. gems/
  37. spring-asciidoctor-backends-0.0.0/
  38. lib/
  39. spring-asciidoctor-backends
  40. data/
  41. ----
  42. NOTE: The version number of the Gem isn't really important in our case so we set it to `0.0.0`.
  43. If you need to edit the `gemspec` file you can find it in link:src/main/ruby[`src/main/gemspec`].
  44. AsciidoctorJ won't find the converter from the Gem alone, it needs to be told that it's a `requireLibrary`.
  45. This is done with the `SpringBackendsExtensionRegistry` class which is registered using `META-INF/services`.
  46. ==== Java Code
  47. The code folding and chomping features are implemented in Java.
  48. You can find the code in link:src/main/java[src/main/java].
  49. There's a `convert_listing_content` method in the Ruby code that uses JRuby features to call the `ListingContentConverters` class.
  50. The `ListingContentConverters` class is responsible for taking an Asciidoctor node and returning its content in a potentially modified form.
  51. There a bit of adapter logic needed to convert the `RubyObject`, but nothing too onerous.
  52. All the project tests are also written in Java.
  53. There's a mixture of both unit tests and integration tests.
  54. They're available at link:src/test/java[src/test/java].
  55. ==== Hacking on the Ruby Code
  56. If you want to iterate quickly on the Ruby code it's often easier to run it directly rather the using the Gradle build.
  57. You can do this by using the `asciidoctor` CLI command.
  58. [source,shell]
  59. ----
  60. $ asciidoctor --trace -r ./src/main/ruby/lib/spring-asciidoctor-backends.rb -b spring-html -D build/converted-asciidoc/ src/test/asciidoc/standard.adoc
  61. ----
  62. NOTE: Using asciidoctor directly means that JRuby doesn't run.
  63. Java calls will not occur and `ListingContentConverters` features will not apply.
  64. === Web Assets
  65. Web assets are bundled up inside the Jar and copied out by the Ruby converter.
  66. The following web assets are used:
  67. * `site.css` file in the `/css` folder.
  68. * `setup.js` and `site.js` files in the `/js` folder.
  69. * Images in the `/img` folder.
  70. ==== Build
  71. The web assets are all build using GulpJS which is invoked via NPM from the Gradle build.
  72. You can also build the web assets directly using the gulp CLI.
  73. Install it using NPM:
  74. [source,shell]
  75. ----
  76. $ npm install
  77. ----
  78. Then run it with the `build` target:
  79. [source,shell]
  80. ----
  81. $ gulp build
  82. ----
  83. The gulp build will do the usual front-end related tasks such as minification and linting.
  84. Take a look `gulpfile.js` if you want to see the exact details.
  85. ==== Javascript
  86. There are two javascript assets produced by the build:
  87. * `setup.js` provides any initial setup and is loaded with a regular `<script>` element.
  88. * `site.js` provides the majority of the Javascript and is loaded with a `<script async>` element.
  89. The source code used to build these files is in link:src/main/js/setup[`src/main/js/setup`] and link:src/main/js/setup[`src/main/js/site`] respectively.
  90. TIP: The `setup.js` file will block page loading and as such should only be used for critical functionality.
  91. For example, it includes the dark theme switching logic so that the page doesn't flash when loading.
  92. The following files are used to create the `setup.js` file:
  93. * `layout.js` - Adds a `js` class to the `html` element for CSS to use for Javascript detection.
  94. * `switchtheme.js` - Provides theme switcher logic
  95. The following files are used to create the `site.js` file:
  96. * `codetools.js` - Provides the copy and fold/unfold buttons
  97. * `highlight.js` - Provide a HighlightJS bundle
  98. * `tabs.js` - Provides tab switching support
  99. * `toc.js` - Updates the table of contents when scrolling
  100. ==== CSS
  101. The single `site.css` file is build from a number of smaller CSS files.
  102. CSS files can be found in link:src/main/css[`src/main/css`].
  103. PostCSS is used to follow `@Import` declarations and create a single file.
  104. Although it's a little overkill for this project, CSS files are organized using the https://www.freecodecamp.org/news/managing-large-s-css-projects-using-the-inverted-triangle-architecture-3c03e4b1e6df/[inverted triangle] architecture.
  105. ----
  106. -------------- _
  107. \ / __ settings : (settings.css, settings-dark.css)
  108. \ / ___ tools : (none)
  109. \ / ____ generic : (generic.css)
  110. \ / _____ elements : (elements.css)
  111. \ / ______ objects : (none)
  112. \ / _______ components : (components.css)
  113. \/ _________utilities : (none)
  114. ----
  115. ===== CSS Variables
  116. CSS variables are used extensively for colors and variable styling.
  117. The `settings.css` file contains initial settings and `settings-dark.css` provides dark-mode overrides.
  118. Variables are usually scoped to a specific area and then reference a more generic value.
  119. For example, `--toc-font-color` is the font color of the TOC.
  120. It has the value `var(--body-font-color)` which references the general body font color.
  121. ==== Images
  122. Images are available in the link:src/main/img[`src/main/img`] folder.
  123. It's best to use SVG images as much as possible.
  124. A single `octicons-16.svg` file provides all the 16x16 icons used by the CSS.
  125. This file contains a subset of the icons from https://github.com/primer/octicons.
  126. ==== Hacking on the web assets
  127. If you need to work on the web assets you can run the following Gradle task:
  128. [source,shell]
  129. ----
  130. $ ./gradlew dev
  131. ----
  132. Alternatively you can generate some test HTML and the run `gulp` directly:
  133. [source,shell]
  134. ----
  135. $ ./gradlew convertTestAsciidoc
  136. $ gulp dev
  137. ----
  138. NOTE: The `convertTestAsciidoc` Gradle task will convert the contents of the link:src/test/asciidoc[`src/test/asciidoc`] directory.
  139. The `dev` task will start a server at `http://localhost:8080` and watch the source files for changes.
  140. Saving any source file will trigger a rebuild and "live reload".
  141. === Formatting
  142. Java, Javascipt and CSS files can be formatted by running `./gradlew format`.
  143. Java files are formatted using `spring-javaformat`.
  144. Other files are formatted using https://prettier.io/[prettier].