-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Support For LaTeX in Docs (#125)
* Add some latex for MathJax testing. * Improve forward and backward Javadoc. * Texify Everything (#124) Adds the ability to specify LaTeX in Javadoc. Adds a GitHub action to automatically parse and inject latex into Javadocs published on the GitHub pages website.
- Loading branch information
1 parent
5524628
commit 05f704d
Showing
64 changed files
with
2,623 additions
and
1,278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ name: Build and Deploy Javadoc | |
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
branches: | ||
- master | ||
jobs: | ||
# 1) Build the Javadoc and upload as artifact | ||
build-docs: | ||
|
@@ -11,35 +12,35 @@ jobs: | |
- name: Check out code | ||
uses: actions/[email protected] | ||
|
||
# 1.1) Setup Java | ||
- name: Set up JDK 23 | ||
uses: actions/[email protected] | ||
with: | ||
distribution: 'zulu' | ||
java-version: 23 | ||
|
||
# 1.2) Setup python | ||
- name: Set up Python | ||
uses: actions/[email protected] | ||
with: | ||
python-version: '3.x' | ||
|
||
# 1.3) Build Javadoc | ||
- name: Build Javadoc | ||
run: | | ||
mvn clean install | ||
mvn clean install -DskipTests | ||
mvn javadoc:javadoc \ | ||
-Dmaven.javadoc.failOnError=false \ | ||
-Dmaven.javadoc.skip=false | ||
# - name: Inject MathJax script | ||
# # inject MathJax for rendering | ||
|
||
# run: | | ||
# find target/reports/apidocs -type f -name "*.html" -exec \ | ||
# sed -i '/<\/head>/i <script type="text/javascript" id="MathJax-script" async \ | ||
# src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"> \ | ||
# </script>' {} + | ||
# 1.5) Find and convert all specified HTML equations to LaTeX equations and inject MathJax script | ||
- name: Convert LaTeX | ||
run: python scripts/convert_latex.py | ||
|
||
# 1.6) Upload generated docs as artifact | ||
- name: Upload Pages Artifact | ||
# This action collects the directory containing docs | ||
# and makes it available as an artifact for later jobs | ||
uses: actions/[email protected] | ||
with: | ||
# Path to the folder containing generated docs | ||
# Adjust path to match your actual Javadoc output | ||
path: target/reports/apidocs | ||
|
||
# 2) Deploy the artifact to GitHub Pages | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ qodana.yaml | |
# Build files and docs | ||
/target/ | ||
/docs/ | ||
/local_scripts/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
# $ \LaTeX $ in Flag4j Javadoc | ||
|
||
--- | ||
|
||
## 1. What!? $ \LaTeX $ in Javadoc? | ||
|
||
That's right! Your eyes do not deceive! | ||
Flag4j supports using $ \LaTeX $ in the web-based Javadoc API. How is this achieved you may ask? | ||
By injecting the MathJax script into all HTML files generated by the Javadoc doclet. Then | ||
a script is run to replace HTML based equations with $ \LaTeX $ equations to be rendered by MathJax. | ||
The Javadoc comments must be formatted correctly for this to work. | ||
|
||
## 2. Doc Comment Syntax | ||
There are four ways to specify that an HTML equation/symbol should be replaced with a | ||
$ \LaTeX $ equation/symbol. [Simple inline replacements](#21-simple-inline--latex--replacements), | ||
[simple display-mode replacements](#22-simple-display--latex--replacements), | ||
[aligned replacements](#23-aligned--latex--replacements), | ||
and [custom replacements](#24-customized--latex--replacements). | ||
|
||
### 2.1 Simple Inline $ \LaTeX $ Replacements | ||
For simple inline html-to-$ \LaTeX $ replacements, the content to be replaced may | ||
be placed within a `<span class="latex-inline">...</span>` block. This will replace | ||
the content in the block with `\( .... \)` and convert any sub/superscripts, common mathematical | ||
symbols, and greek letters from html form to $ \LaTeX $. For a full list of all replacements made for | ||
simple latex replacements, see [A1](#a1-simple--latex--regex-replacements). | ||
|
||
`<pre>...</pre>` will be discarded when making the conversion. | ||
Simple fractions of the form `(...) / (...) ` are also supported. | ||
The parentheses are required for proper conversion. | ||
|
||
The following docstring, | ||
```java | ||
/** | ||
* The equation <span class="latex-inline">y = &alpha;x + b</span> | ||
* will be solved for <span class="latex-inline">x</span>. | ||
* | ||
* <span class="latex-inline">(3x<sup>2</sup>) / (&radic;(2y - 1))</span> | ||
*/ | ||
``` | ||
|
||
will be converted to the following in the final HTML, | ||
```html | ||
The equation \( y = \alpha x + b \) | ||
will be solved for \( x \). | ||
|
||
\( \cfrac{3x^{2}}{\sqrt{2y - 1}} \) | ||
``` | ||
--- | ||
|
||
### 2.2 Simple Display $ \LaTeX $ Replacements | ||
|
||
To render $ \LaTeX $ in display mode, use the tags | ||
`<span class="latex-display">...</span>` | ||
|
||
The same rules apply as in the inline rendering. | ||
|
||
The following docstring, | ||
```java | ||
/** | ||
* The equation <span class="latex-display">y = &alpha;x + b</span> | ||
* will be solved for <span class="latex-inline">x</span>. | ||
* | ||
* <span class="latex-display">(3x<sup>2</sup>) / (&radic;(2y - 1))</span> | ||
*/ | ||
``` | ||
|
||
will be converted to the following in the final HTML, | ||
```html | ||
The equation \[ y = \alpha x + b \] | ||
will be solved for \( x \). | ||
|
||
\[ \cfrac{3x^{2}}{\sqrt{2y - 1}} \] | ||
``` | ||
|
||
--- | ||
|
||
### 2.3 Aligned $ \LaTeX $ Replacements | ||
|
||
If you would like to align a multi-line HTML equation by equal or "implies" signs, use | ||
the tags `<span class="latex-eq-align">...</span>`. There must be exactly one '=' | ||
per line of the equation. Similarly, `<span class="latex-impl-align">...</span>` can | ||
be used for implication chains. | ||
|
||
The following docstring, | ||
```java | ||
/** | ||
* <span class="latex-eq-align"> | ||
* <pre> | ||
* A = T B T<sup>-1</sup> | ||
* = P D B D<sup>-1</sup> P<sup>-1</sup></pre> | ||
* </span> | ||
* | ||
* <span class="latex-impl-align"> | ||
* <pre> | ||
* x < α &Implies; x < &Sigma;<sub>i=1</sub><sup>N</sup> (y<sub>i</sub>); | ||
* &Implies; x < &beta;</pre> | ||
* </span> | ||
*/ | ||
``` | ||
|
||
will be converted to the following in the final HTML, | ||
```html | ||
\[ \begin{align*} A &= T B T^{-1} \\ | ||
&= P D B D^{-1} P^{-1}. \end{align*} \] | ||
|
||
\[ \begin{align*} x < \alpha &\implies x < \sum_{i=1}^{N} \left (y_{i}\right ) \\ | ||
&\implies x < \beta \end{align*} \] | ||
``` | ||
|
||
--- | ||
|
||
### 2.4 Customized $ \LaTeX $ Replacements | ||
For more complicated $ \LaTeX $ Replacements we may want to specify <em>exactly</em> what | ||
the $ \LaTeX $ should be replacing the HTML. To do this we utilize the `<span | ||
class="latex-replace">...</span>` block. | ||
|
||
To specify custom $ \LaTeX $ you must provide an HTML equation for simple rendering within an IDE. | ||
This allows users to avoid seeing raw $ \LaTeX $ in the docs rendered by the IDE. | ||
|
||
You can then optionally provide a $ \LaTeX $ equation within an HTML comment to replace the preceding HTML equation. | ||
|
||
#### 2.1.1 Specifying An HTML Equation Is "$ \LaTeX $ Replaceable" | ||
The following doc comment specifies that an equation is replaceable | ||
```java | ||
/** | ||
* <span class="latex-replace"> | ||
* <pre> | ||
* <sup> </sup>[ T<sub>1</sub> X*D<sub>1</sub> Y ] | ||
* D<sup>-1</sup> P<sup>-1</sup> A P D = [ 0 D<sub>1</sub><sup>-1</sup>*B<sub>1</sub>*D<sub>1</sub> D<sub>1</sub><sup>-1</sup>*Z ] | ||
* <sup> </sup>[ 0 0 T<sub>2</sub> ]</pre> | ||
* </span> | ||
*/ | ||
``` | ||
The `<span class="latex-replace">` tag indicates that the following HTML equation may be replaced by | ||
a $ \LaTeX $ equation when building the docs. This tag and the closing `</span>` <em>must</em> | ||
be included for the equation to be replaced. If there is not a latex equations following this, the | ||
tag will be ignored. | ||
|
||
Providing the equation as HTML allows The InteliJ IDE to render the equations as: | ||
|
||
 | ||
|
||
This looks okay, but we know that $ \LaTeX $ could look <em>SO</em> much better! | ||
|
||
#### 2.1.2 Specifying A Custom $ \LaTeX $ Equation | ||
We do not want the $ \LaTeX $ equation to rendered in an IDE as raw text. To avoid this, we place | ||
the equation inside a special HTML comment. The comment must begin with `<!-- LATEX:`. | ||
```java | ||
/** | ||
* <!-- LATEX: | ||
* $$ \begin{bmatrix} | ||
* T_1 & XD_1 & Y \\ | ||
* \mathbf{0} & D_1^{-1}B_1D_1 & D_1^{-1}Z \\ | ||
* \mathbf{0} & \mathbf{0} & T_2 | ||
* \end{bmatrix} $$ | ||
* --> | ||
*/ | ||
``` | ||
|
||
The HTML comment `<!-- LATEX: ... -->` must immediately follow the `<span | ||
class="latex-replace">...</span>` | ||
block for it to be utilized (white space and newlines are allowed between the two). | ||
Otherwise, the $ \LaTeX $ comment will be ignored. | ||
|
||
The single '\$' character is not supported for inline $ \LaTeX $ (i.e. `$ ... $`) in default MathJax. | ||
Instead, use `\( ... \)` for inline equations. Both ``$$ ... $$`` and `\[ ... \]` may be used for display mode. | ||
|
||
#### 2.1.3 Full Example | ||
The full doc comment would look something like, | ||
```java | ||
/** | ||
* Some stuff... | ||
* | ||
* <span class="latex-replace"> | ||
* <pre> | ||
* <sup> </sup>[ T<sub>1</sub> X*D<sub>1</sub> Y ] | ||
* D<sup>-1</sup> P<sup>-1</sup> A P D = [ 0 D<sub>1</sub><sup>-1</sup>*B<sub>1</sub>*D<sub>1</sub> D<sub>1</sub><sup>-1</sup>*Z ] | ||
* <sup> </sup>[ 0 0 T<sub>2</sub> ]</pre> | ||
* </span> | ||
* | ||
* | ||
* <!-- LATEX: | ||
* $$ \begin{bmatrix} | ||
* T_1 & XD_1 & Y \\ | ||
* \mathbf{0} & D_1^{-1}B_1D_1 & D_1^{-1}Z \\ | ||
* \mathbf{0} & \mathbf{0} & T_2 | ||
* \end{bmatrix} $$ | ||
* --> | ||
* | ||
* Some more stuff... | ||
*/ | ||
``` | ||
|
||
The exact regex used in python to match such instances is: | ||
```regexp | ||
(<span class="latex-replace">.*?</span>)\s*<!-- LATEX:\s*(\{@literal\s*.*?\s*}\s*) --> | ||
``` | ||
where `.*` will match <em>any</em> character including newlines. If the text in the generated | ||
HTML does not match this, no replacements will be made. | ||
|
||
In an IDE, the $ \LaTeX $ in the comment will <em>not</em> be rendered. The user will <em>only</em> see the | ||
HTML equation. Once the Javadocs are built, parsed, and deployed to the [Flag4j API website](https://jacobdwatters.github.io/Flag4j/), | ||
the HTML equation will be fully replaced with the $ \LaTeX $ equation and MathJax will render it as: | ||
|
||
 | ||
|
||
Wow! That looks so much better! Enjoy your $ \LaTeX $. | ||
|
||
# Appendix | ||
## A.1 Simple $ \LaTeX $ Regex Replacements | ||
This is the full regex replacement mapping for simple latex tags: `<span class=simple-inline>`, | ||
`<span class=simple-display>`, `<span class=latex-eq-align>`, `<span class=latex-impl-align>`. | ||
|
||
Replacements are performed in the same order they appear in the mapping. | ||
|
||
```python | ||
html2latex = { | ||
# Braces | ||
r"\{": r"\\left {", r"\}": r"\\right }", | ||
|
||
# Simple fractions | ||
r"\([ ]*([^()<>]+)[ ]*\)[ ]*/[ ]*\([ ]*([^()<>]+)[ ]*\)": r"\\cfrac{\g<1>}{\g<2>}", | ||
|
||
# Parens, brackets | ||
r"\(": r"\\left (", r"\)": r"\\right )", r"\[": r"\\left [", r"]": r"\\right ]", | ||
|
||
# Sums and products | ||
r"&Sigma;<sub>(.*?)</sub><sup>(.*?)</sup>": r"\\sum_{\g<1>}^{\g<2>}", | ||
r"&Pi;<sub>(.*?)</sub><sup>(.*?)</sup>": r"\\prod_{\g<1>}^{\g<2>}", | ||
|
||
# Lowercase greek | ||
r"&alpha;": r"\\alpha ", r"&beta;": r"\\beta ", r"&gamma;": r"\\gamma ", r"&delta;": r"\\delta ", r"&epsilon;": r"\\epsilon ", | ||
r"&zeta;": r"\\zeta ", r"&eta;": r"\\eta ", r"&theta;": r"\\theta ", r"&iota;": r"\\iota ", r"&kappa;": r"\\kappa ", | ||
r"&lambda;": r"\\lambda ", r"&mu;": r"\\mu ", r"&nu;": r"\\nu ", r"&xi;": r"\\xi ", r"&omicron;": r"\\omicron ", r"&pi;": r"\\pi ", | ||
r"&rho;": r"\\rho ", r"&sigma;": r"\\sigma ", r"&tau;": r"\\tau ", r"&upsilon;": r"\\upsilon ", r"&phi;": r"\\phi ", | ||
r"&chi;": r"\\chi ", r"&psi;": r"\\psi ", r"&omega;": r"\\omega ", | ||
|
||
# Uppercase greek | ||
r"&Alpha;": r"\\Alpha ", r"&Beta;": r"\\Beta ", r"&Gamma;": r"\\Gamma ", r"&Delta;": r"\\Delta ", r"&Epsilon;": r"\\Epsilon ", | ||
r"&Zeta;": r"\\Zeta ", r"&Eta;": r"\\Eta ", r"&Theta;": r"\\Theta ", r"&Iota;": r"\\Iota ", r"&Kappa;": r"\\Kappa ", | ||
r"&Lambda;": r"\\Lambda ", r"&Mu;": r"\\Mu ", r"&Nu;": r"\\Nu ", r"&Xi;": r"\\Xi ", r"&Omicron;": r"\\Omicron ", r"&Pi;": r"\\Pi ", | ||
r"&Rho;": r"\\Rho ", r"&Sigma;": r"\\Sigma ", r"&Tau;": r"\\Tau ", r"&Upsilon;": r"\\Upsilon ", r"&Phi;": r"\\Phi ", | ||
r"&Chi;": r"\\Chi ", r"&Psi;": r"\\Psi ", r"&Omega;": r"\\Omega ", | ||
|
||
# Sub/superscripts | ||
r"<sub>": r"_{", "</sub>": "}", r"<sup>": r"^{", "</sup>": r"}", | ||
|
||
# Boldface | ||
r"<b>(.*?)</b>": r"\\mathbf{\g<1>}", r"<strong>(.*?)</strong>": r"\\mathbf{\g<1>}", | ||
|
||
# Common sets. | ||
r"ℝ": r"\\mathbb{R}", r"ℚ": r"\\mathbb{Q}", r"ℂ": r"\\mathbb{C}", r"ℤ": r"\\mathbb{Z}", r"ℕ": r"\\mathbb{N}", | ||
|
||
# Operators | ||
r"&lt;": r"<", r"&gt;": r">", r"&le;": r"\\leq ", r"&ge;": r"\\geq ", r"&ne;": r"\\neq ", r"&plusmn;": r"\\pm ", | ||
r"&isin;": r"\\in ", r"&notin;": r"\\notin ", r"&radic;\((.*)?\)": r"\\sqrt{\g<1>}", r"≈": r"\\approx ", | ||
r"&oplus;": r"\\oplus ", r"&Implies;": r"\\implies", r"&times;": r"\\times", r"&middot;": r"\\cdot", | ||
|
||
# Other symbols. | ||
r"&infin;": r"\\inf ", r"\.\.\.": r"\\cdots ", r"&ell;": r"\\ell " | ||
} | ||
``` |
Oops, something went wrong.