Skip to content

Commit 30f2576

Browse files
committed
allow per-element option overriding
1 parent 39e43af commit 30f2576

File tree

7 files changed

+32
-12
lines changed

7 files changed

+32
-12
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,10 @@ To display the caption of an algorithm, use `algorithm` environment as a 'float'
241241
```
242242

243243
### Options
244-
`pseudocode.renderElement` can accept an option as the last argument,
245-
such as
244+
245+
#### Global Options
246+
247+
`pseudocode.renderElement` can accept an option object as the last argument, such as
246248

247249
```js
248250
pseudocode.renderElement(document.getElementById("quicksort"),
@@ -274,6 +276,21 @@ var DEFAULT_OPTIONS = {
274276
};
275277
```
276278

279+
#### Per-Element Options
280+
281+
The above-mentioned global options may be overridden on a per-element basis
282+
using [HTML `data-*` attributes](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset)
283+
on the `<pre>` DOM element.
284+
285+
The following example demonstrates how to enable line numbers and change title prefix:
286+
287+
```html
288+
<pre id="quicksort" class="pseudocode" style="display:hidden;"
289+
data-line-number=true data-title-prefix="Algo">
290+
...
291+
</pre>
292+
```
293+
277294
## Build and Test
278295
pseudocode.js is written in JavaScript and built with [Node.js](https://nodejs.org).
279296
So, make sure you have Node.js installed before building pseudocode.js.

docs/katex-samples.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</head>
1818

1919
<body>
20-
<pre id="test-basics" style="display:none">
20+
<pre id="test-basics" style="display:none" data-line-number=true>
2121
\begin{algorithm}
2222
\caption{Test text-style}
2323
\begin{algorithmic}
@@ -141,7 +141,7 @@
141141
\end{algorithmic}
142142
\end{algorithm}
143143
</pre>
144-
<pre id="test-examples" style="display:none">
144+
<pre id="test-examples" style="display:none" data-title-prefix="Procedure">
145145
% This quicksort algorithm is extracted from Chapter 7, Introduction
146146
% to Algorithms (3rd edition)
147147
\begin{algorithm}

docs/mathjax-v2-samples.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</head>
2929

3030
<body>
31-
<pre id="test-basics" style="display:none">
31+
<pre id="test-basics" style="display:none" data-line-number=true>
3232
\begin{algorithm}
3333
\caption{Test text-style}
3434
\begin{algorithmic}
@@ -152,7 +152,7 @@
152152
\end{algorithmic}
153153
\end{algorithm}
154154
</pre>
155-
<pre id="test-examples" style="display:none">
155+
<pre id="test-examples" style="display:none" data-title-prefix="Procedure">
156156
% This quicksort algorithm is extracted from Chapter 7, Introduction
157157
% to Algorithms (3rd edition)
158158
\begin{algorithm}

docs/mathjax-v3-samples.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</head>
2828

2929
<body>
30-
<pre id="test-basics" style="display:none">
30+
<pre id="test-basics" style="display:none" data-line-number=true>
3131
\begin{algorithm}
3232
\caption{Test text-style}
3333
\begin{algorithmic}
@@ -151,7 +151,7 @@
151151
\end{algorithmic}
152152
\end{algorithm}
153153
</pre>
154-
<pre id="test-examples" style="display:none">
154+
<pre id="test-examples" style="display:none" data-title-prefix="Procedure">
155155
% This quicksort algorithm is extracted from Chapter 7, Introduction
156156
% to Algorithms (3rd edition)
157157
\begin{algorithm}

docs/pseudocode.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pseudocode.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ module.exports = {
4747

4848
elem.style.display = 'none';
4949

50-
var R = makeRenderer(elem.textContent, options);
50+
var elemOptions = JSON.parse(JSON.stringify(options));
51+
for (const dataProp in elem.dataset)
52+
elemOptions[dataProp] = elem.dataset[dataProp];
53+
var R = makeRenderer(elem.textContent, elemOptions);
5154

5255
var newElem = R.toDOM();
5356
elem.replaceWith(newElem);

static/body.html.part

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
</head>
22

33
<body>
4-
<pre id="test-basics" style="display:none">
4+
<pre id="test-basics" style="display:none" data-line-number=true>
55
\begin{algorithm}
66
\caption{Test text-style}
77
\begin{algorithmic}
@@ -125,7 +125,7 @@
125125
\end{algorithmic}
126126
\end{algorithm}
127127
</pre>
128-
<pre id="test-examples" style="display:none">
128+
<pre id="test-examples" style="display:none" data-title-prefix="Procedure">
129129
% This quicksort algorithm is extracted from Chapter 7, Introduction
130130
% to Algorithms (3rd edition)
131131
\begin{algorithm}

0 commit comments

Comments
 (0)