Skip to content

Commit

Permalink
[no ci] Update highlightjs-line-numbers to use mdbook supported ver…
Browse files Browse the repository at this point in the history
…sion.
  • Loading branch information
azriel91 committed Dec 1, 2023
1 parent bf4664f commit 8a1e22b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 127 deletions.
40 changes: 3 additions & 37 deletions doc/src/css/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,11 @@ hr {
}

/* === highlightjs code === */
code.hljs.language-undefined {
:not(pre) > code.hljs {
background-color: #e0e0e0;
}

/* === highlightjs-line-numbers === */
/* main table */

.hljs-ln > tbody > tr:nth-child(2n) {
background: none; /* undo general.css colouring */
}
.hljs-ln {
display: flex;
align-items: flex-start;
}

.hljs-ln-line {
.hljs-ln-n {
border: none;
padding: 0.1em 0.5em; /* undo general.css spacing */
}

/* for block of numbers */
.hljs-ln-numbers {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

text-align: right;
color: #aaaaaa;
border-right: 1px solid #bbbbbb;
vertical-align: top;
padding-right: 5px;

/* your custom style here */
}

/* for block of code */
.hljs-ln-line .hljs-ln-code {
flex-grow: 1;
padding: 0.1em 0.5em;
}
29 changes: 19 additions & 10 deletions doc/src/js/highlightjs-line-numbers.min.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
// Taken from https://cdn.jsdelivr.net/npm/[email protected]/dist/highlightjs-line-numbers.min.js
//
// Notably, I made the following changes:
//
// * Removed `td{padding:0}` from the element inline style, just before `.{1}:before`, so that styles in `layout.css` will work.
// * Changed `t[o]:" "` to `t[o]:"<br/>"`, so that copied code has an empty newline instead of a line with a space character.
!function(r,o){"use strict";var e,i="hljs-ln",l="hljs-ln-line",h="hljs-ln-code",s="hljs-ln-numbers",c="hljs-ln-n",m="data-line-number",a=/\r\n|\r|\n/g;function u(e){for(var n=e.toString(),t=e.anchorNode;"TD"!==t.nodeName;)t=t.parentNode;for(var r=e.focusNode;"TD"!==r.nodeName;)r=r.parentNode;var o=parseInt(t.dataset.lineNumber),a=parseInt(r.dataset.lineNumber);if(o==a)return n;var i,l=t.textContent,s=r.textContent;for(a<o&&(i=o,o=a,a=i,i=l,l=s,s=i);0!==n.indexOf(l);)l=l.slice(1);for(;-1===n.lastIndexOf(s);)s=s.slice(0,-1);for(var c=l,u=function(e){for(var n=e;"TABLE"!==n.nodeName;)n=n.parentNode;return n}(t),d=o+1;d<a;++d){var f=p('.{0}[{1}="{2}"]',[h,m,d]);c+="\n"+u.querySelector(f).textContent}return c+="\n"+s}function n(e){try{var n=o.querySelectorAll("code.hljs,code.nohighlight");for(var t in n)n.hasOwnProperty(t)&&(n[t].classList.contains("nohljsln")||d(n[t],e))}catch(e){r.console.error("LineNumbers error: ",e)}}function d(e,n){"object"==typeof e&&r.setTimeout(function(){e.innerHTML=f(e,n)},0)}function f(e,n){var t,r,o=(t=e,{singleLine:function(e){return!!e.singleLine&&e.singleLine}(r=(r=n)||{}),startFrom:function(e,n){var t=1;isFinite(n.startFrom)&&(t=n.startFrom);var r=function(e,n){return e.hasAttribute(n)?e.getAttribute(n):null}(e,"data-ln-start-from");return null!==r&&(t=function(e,n){if(!e)return n;var t=Number(e);return isFinite(t)?t:n}(r,1)),t}(t,r)});return function e(n){var t=n.childNodes;for(var r in t){var o;t.hasOwnProperty(r)&&(o=t[r],0<(o.textContent.trim().match(a)||[]).length&&(0<o.childNodes.length?e(o):v(o.parentNode)))}}(e),function(e,n){var t=g(e);""===t[t.length-1].trim()&&t.pop();if(1<t.length||n.singleLine){for(var r="",o=0,a=t.length;o<a;o++)r+=p('<tr><td class="{0} {1}" {3}="{5}"><div class="{2}" {3}="{5}"></div></td><td class="{0} {4}" {3}="{5}">{6}</td></tr>',[l,s,c,m,h,o+n.startFrom,0<t[o].length?t[o]:"<br/>"]);return p('<table class="{0}">{1}</table>',[i,r])}return e}(e.innerHTML,o)}function v(e){var n=e.className;if(/hljs-/.test(n)){for(var t=g(e.innerHTML),r=0,o="";r<t.length;r++){o+=p('<span class="{0}">{1}</span>\n',[n,0<t[r].length?t[r]:" "])}e.innerHTML=o.trim()}}function g(e){return 0===e.length?[]:e.split(a)}function p(e,t){return e.replace(/\{(\d+)\}/g,function(e,n){return void 0!==t[n]?t[n]:e})}r.hljs?(r.hljs.initLineNumbersOnLoad=function(e){"interactive"===o.readyState||"complete"===o.readyState?n(e):r.addEventListener("DOMContentLoaded",function(){n(e)})},r.hljs.lineNumbersBlock=d,r.hljs.lineNumbersValue=function(e,n){if("string"!=typeof e)return;var t=document.createElement("code");return t.innerHTML=e,f(t,n)},(e=o.createElement("style")).type="text/css",e.innerHTML=p(".{0}{border-collapse:collapse}.{0} .{1}:before{content:attr({2})}",[i,c,m]),o.getElementsByTagName("head")[0].appendChild(e)):r.console.error("highlight.js not detected!"),document.addEventListener("copy",function(e){var n,t=window.getSelection();!function(e){for(var n=e;n;){if(n.className&&-1!==n.className.indexOf("hljs-ln-code"))return 1;n=n.parentNode}}(t.anchorNode)||(n=-1!==window.navigator.userAgent.indexOf("Edge")?u(t):t.toString(),e.clipboardData.setData("text/plain",n),e.preventDefault())})}(window,document);

// Also added this line
hljs.initLineNumbersOnLoad();
!function(r,i){"use strict";var n,c="hljs-ln",u="hljs-ln-line",a="hljs-ln-n",h=/\r\n|\r|\n/g;function e(n){"interactive"===i.readyState||"complete"===i.readyState?t(n):r.addEventListener("DOMContentLoaded",function(){t(n)})}function t(n){try{var e,t=i.querySelectorAll("code.hljs,code.nohighlight");for(e in t)!t.hasOwnProperty(e)||t[e].classList.contains("nohljsln")||l(t[e],n)}catch(n){r.console.error("LineNumbers error: ",n)}}function l(n,e){"object"==typeof n&&r.setTimeout(function(){n.innerHTML=o(n,e)},0)}function o(n,e){var e={singleLine:function(n){return n.singleLine||!1}(e=(e=e)||{}),startFrom:function(n,e){var t=0;isFinite(e.startFrom)&&(t=e.startFrom);e=function(n,e){return n.hasAttribute(e)?n.getAttribute(e):null}(n,"data-ln-start-from");null!==e&&(t=function(n,e){return n&&(n=Number(n),isFinite(n))?n:e}(e,0));return t}(n,e)},n=n.innerHTML,t=function(n){return 0===n.length?[]:n.split(h)}(n);if(""===t[t.length-1].trim()&&t.pop(),1<t.length||e.singleLine){for(var r="",i=Math.trunc(Math.log10(t.length)),l=0,o=t.length;l<o;l++){var s="";t[l].includes("boring")&&(s="boring"),r+=`<div class="${u}"
><div
class="${a} ${s}"
style="width: ${i}em;"
></div>${0<t[l].length?t[l]:"&#13;"}</div>`}return`<div
class="${c}"
style="counter-reset: line-number-count ${e.startFrom}"
>${r}</div>`}return n}r.hljs?(r.hljs.initLineNumbersOnLoad=e,r.hljs.lineNumbersBlock=l,r.hljs.lineNumbersValue=function(n,e){var t;if("string"==typeof n)return(t=document.createElement("code")).innerHTML=n,o(t,e)},(n=i.createElement("style")).type="text/css",n.innerHTML=`
.${c} { display: block; }
.${u} { counter-increment: line-number-count; }
.${a} {
display: inline-block;
text-align: right;
margin-right: 2.0rem;
}
.${a}:before {
content: counter(line-number-count);
}
`,i.getElementsByTagName("head")[0].appendChild(n),e()):r.console.error("highlight.js not detected!")}(window,document);
154 changes: 74 additions & 80 deletions doc/src/learning_material/interruptible_software/implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## No Interruptibility

```rust ,ignore
# use std::marker;
fn execute(
params: Params,
) -> Outcome {
Expand Down Expand Up @@ -63,15 +64,11 @@ fn execute<T>(

[Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fc67ea9dd630b41858b6bca9a18bd412)


<details>
<summary></summary>

```rust
use std::any::Any;
use std::iter::IntoIterator;
use std::marker::PhantomData;

# use std::any::Any;
# use std::iter::IntoIterator;
# use std::marker::PhantomData;
#
fn main() {
match execute::<String, _>(Receiver::new(3), true) {
Ok(value) => println!("main: {}", value),
Expand Down Expand Up @@ -114,80 +111,77 @@ where
.map(|boxed_any| *boxed_any.downcast().unwrap())
}

fn step<F, I, O>(f: F) -> Box<dyn StepErased>
where
F: Fn(I) -> O + 'static,
I: 'static,
O: 'static,
{
Box::new(StepWrapper::new(f))
}

trait Step {
type Input: Any;
type Output: Any;
fn exec(&self, input: Self::Input) -> Self::Output;
}

impl<F, I, O> Step for StepWrapper<F, I, O>
where
F: Fn(I) -> O,
I: 'static,
O: 'static,
{
type Input = I;
type Output = O;

fn exec(&self, input: Self::Input) -> Self::Output {
(self.0)(input)
}
}

trait StepErased {
fn exec(&self, input: Box<dyn Any>) -> Box<dyn Any>;
}

struct StepWrapper<F, I, O>(F, PhantomData<(I, O)>);
impl<F, I, O> StepWrapper<F, I, O> {
fn new(f: F) -> Self {
Self(f, PhantomData)
}
}

impl<F, I, O> StepErased for StepWrapper<F, I, O>
where
StepWrapper<F, I, O>: Step<Input = I, Output = O>,
I: 'static,
O: 'static,
{
fn exec(&self, input: Box<dyn Any>) -> Box<dyn Any> {
let input = *input.downcast::<I>().unwrap();
let output = Step::exec(self, input);
Box::new(output)
}
}

struct Receiver<T>(u32, PhantomData<T>);
impl<T> Receiver<T> {
pub fn new(n: u32) -> Self {
Self(n, PhantomData)
}
}
#[derive(Debug)]
struct InterruptSignal;
fn interruptibility_check(rx: &mut Receiver<InterruptSignal>) -> Result<(), InterruptSignal> {
if (rx.0) == 0 {
Err(InterruptSignal)
} else {
rx.0 -= 1;
Result::Ok(())
}
}
# fn step<F, I, O>(f: F) -> Box<dyn StepErased>
# where
# F: Fn(I) -> O + 'static,
# I: 'static,
# O: 'static,
# {
# Box::new(StepWrapper::new(f))
# }
#
# trait Step {
# type Input: Any;
# type Output: Any;
# fn exec(&self, input: Self::Input) -> Self::Output;
# }
#
# impl<F, I, O> Step for StepWrapper<F, I, O>
# where
# F: Fn(I) -> O,
# I: 'static,
# O: 'static,
# {
# type Input = I;
# type Output = O;
#
# fn exec(&self, input: Self::Input) -> Self::Output {
# (self.0)(input)
# }
# }
#
# trait StepErased {
# fn exec(&self, input: Box<dyn Any>) -> Box<dyn Any>;
# }
#
# struct StepWrapper<F, I, O>(F, PhantomData<(I, O)>);
# impl<F, I, O> StepWrapper<F, I, O> {
# fn new(f: F) -> Self {
# Self(f, PhantomData)
# }
# }
#
# impl<F, I, O> StepErased for StepWrapper<F, I, O>
# where
# StepWrapper<F, I, O>: Step<Input = I, Output = O>,
# I: 'static,
# O: 'static,
# {
# fn exec(&self, input: Box<dyn Any>) -> Box<dyn Any> {
# let input = *input.downcast::<I>().unwrap();
# let output = Step::exec(self, input);
# Box::new(output)
# }
# }
#
# struct Receiver<T>(u32, PhantomData<T>);
# impl<T> Receiver<T> {
# pub fn new(n: u32) -> Self {
# Self(n, PhantomData)
# }
# }
# #[derive(Debug)]
# struct InterruptSignal;
# fn interruptibility_check(rx: &mut Receiver<InterruptSignal>) -> Result<(), InterruptSignal> {
# if (rx.0) == 0 {
# Err(InterruptSignal)
# } else {
# rx.0 -= 1;
# Result::Ok(())
# }
# }
```

</details>


1. To implement interruptibility, we start with the non-interruptible logic.
2. And we saw earlier we could sprinkle interruptibility checks before each step.
3. But we've lost clarity.
Expand Down

0 comments on commit 8a1e22b

Please sign in to comment.