Skip to content

Commit

Permalink
allow per-element option overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
SaswatPadhi committed May 28, 2023
1 parent 39e43af commit 30f2576
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ To display the caption of an algorithm, use `algorithm` environment as a 'float'
```

### Options
`pseudocode.renderElement` can accept an option as the last argument,
such as

#### Global Options

`pseudocode.renderElement` can accept an option object as the last argument, such as

```js
pseudocode.renderElement(document.getElementById("quicksort"),
Expand Down Expand Up @@ -274,6 +276,21 @@ var DEFAULT_OPTIONS = {
};
```

#### Per-Element Options

The above-mentioned global options may be overridden on a per-element basis
using [HTML `data-*` attributes](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset)
on the `<pre>` DOM element.

The following example demonstrates how to enable line numbers and change title prefix:

```html
<pre id="quicksort" class="pseudocode" style="display:hidden;"
data-line-number=true data-title-prefix="Algo">
...
</pre>
```

## Build and Test
pseudocode.js is written in JavaScript and built with [Node.js](https://nodejs.org).
So, make sure you have Node.js installed before building pseudocode.js.
Expand Down
4 changes: 2 additions & 2 deletions docs/katex-samples.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</head>

<body>
<pre id="test-basics" style="display:none">
<pre id="test-basics" style="display:none" data-line-number=true>
\begin{algorithm}
\caption{Test text-style}
\begin{algorithmic}
Expand Down Expand Up @@ -141,7 +141,7 @@
\end{algorithmic}
\end{algorithm}
</pre>
<pre id="test-examples" style="display:none">
<pre id="test-examples" style="display:none" data-title-prefix="Procedure">
% This quicksort algorithm is extracted from Chapter 7, Introduction
% to Algorithms (3rd edition)
\begin{algorithm}
Expand Down
4 changes: 2 additions & 2 deletions docs/mathjax-v2-samples.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</head>

<body>
<pre id="test-basics" style="display:none">
<pre id="test-basics" style="display:none" data-line-number=true>
\begin{algorithm}
\caption{Test text-style}
\begin{algorithmic}
Expand Down Expand Up @@ -152,7 +152,7 @@
\end{algorithmic}
\end{algorithm}
</pre>
<pre id="test-examples" style="display:none">
<pre id="test-examples" style="display:none" data-title-prefix="Procedure">
% This quicksort algorithm is extracted from Chapter 7, Introduction
% to Algorithms (3rd edition)
\begin{algorithm}
Expand Down
4 changes: 2 additions & 2 deletions docs/mathjax-v3-samples.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</head>

<body>
<pre id="test-basics" style="display:none">
<pre id="test-basics" style="display:none" data-line-number=true>
\begin{algorithm}
\caption{Test text-style}
\begin{algorithmic}
Expand Down Expand Up @@ -151,7 +151,7 @@
\end{algorithmic}
\end{algorithm}
</pre>
<pre id="test-examples" style="display:none">
<pre id="test-examples" style="display:none" data-title-prefix="Procedure">
% This quicksort algorithm is extracted from Chapter 7, Introduction
% to Algorithms (3rd edition)
\begin{algorithm}
Expand Down
2 changes: 1 addition & 1 deletion docs/pseudocode.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pseudocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ module.exports = {

elem.style.display = 'none';

var R = makeRenderer(elem.textContent, options);
var elemOptions = JSON.parse(JSON.stringify(options));
for (const dataProp in elem.dataset)
elemOptions[dataProp] = elem.dataset[dataProp];
var R = makeRenderer(elem.textContent, elemOptions);

var newElem = R.toDOM();
elem.replaceWith(newElem);
Expand Down
4 changes: 2 additions & 2 deletions static/body.html.part
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
</head>

<body>
<pre id="test-basics" style="display:none">
<pre id="test-basics" style="display:none" data-line-number=true>
\begin{algorithm}
\caption{Test text-style}
\begin{algorithmic}
Expand Down Expand Up @@ -125,7 +125,7 @@
\end{algorithmic}
\end{algorithm}
</pre>
<pre id="test-examples" style="display:none">
<pre id="test-examples" style="display:none" data-title-prefix="Procedure">
% This quicksort algorithm is extracted from Chapter 7, Introduction
% to Algorithms (3rd edition)
\begin{algorithm}
Expand Down

0 comments on commit 30f2576

Please sign in to comment.