-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor code to allow TikZ to html conversions on demand #30
Open
rei2hu
wants to merge
5
commits into
kisonecat:master
Choose a base branch
from
rei2hu:chore/refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
node_modules | ||
package-lock.json | ||
*.swp |
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
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
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 |
---|---|---|
@@ -1,123 +1,75 @@ | ||
import { dvi2html } from 'dvi2html'; | ||
import { Writable } from 'stream'; | ||
import * as library from './library'; | ||
import pako from 'pako'; | ||
import fetchStream from 'fetch-readablestream'; | ||
|
||
// document.currentScript polyfill | ||
if (document.currentScript === undefined) { | ||
var scripts = document.getElementsByTagName('script'); | ||
document.currentScript = scripts[scripts.length - 1]; | ||
} | ||
|
||
// Determine where we were loaded from; we'll use that to find a | ||
// tikzwolke server that can handle our POSTing tikz code | ||
var url = new URL(document.currentScript.src); | ||
// host includes the port | ||
var host = url.host; | ||
var urlRoot = url.protocol + '//' + host; | ||
|
||
let pages = 1000; | ||
var coredump; | ||
var code; | ||
|
||
async function load() { | ||
let tex = await fetch(urlRoot + '/tex.wasm'); | ||
code = await tex.arrayBuffer(); | ||
|
||
let response = await fetchStream(urlRoot + '/core.dump.gz'); | ||
const reader = response.body.getReader(); | ||
const inf = new pako.Inflate(); | ||
|
||
try { | ||
while (true) { | ||
const {done, value} = await reader.read(); | ||
inf.push(value, done); | ||
if (done) break; | ||
} | ||
} | ||
finally { | ||
reader.releaseLock(); | ||
} | ||
|
||
coredump = new Uint8Array( inf.result, 0, pages*65536 ); | ||
import { dvi2html } from "dvi2html"; | ||
import { Writable } from "stream"; | ||
import * as library from "./library"; | ||
import pako from "pako"; | ||
import fetchStream from "fetch-readablestream"; | ||
|
||
const pages = 1000; | ||
|
||
export async function load(wasmUrl, coredumpUrl) { | ||
const tex = await fetch(wasmUrl); | ||
const code = await tex.arrayBuffer(); | ||
|
||
const response = await fetchStream(coredumpUrl); | ||
const reader = response.body.getReader(); | ||
const inf = new pako.Inflate(); | ||
|
||
try { | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
inf.push(value, done); | ||
if (done) break; | ||
} | ||
} | ||
finally { | ||
reader.releaseLock(); | ||
} | ||
|
||
const coredump = new Uint8Array(inf.result, 0, pages*65536); | ||
return async (input) => { | ||
if (input.match("\\\\begin *{document}") === null) { | ||
input = "\\begin{document}\n" + input; | ||
} | ||
input = input + "\n\\end{document}\n"; | ||
|
||
library.deleteEverything(); | ||
library.writeFileSync("sample.tex", Buffer.from(input)); | ||
|
||
const memory = new WebAssembly.Memory({ initial: pages, maximum: pages }); | ||
|
||
const buffer = new Uint8Array(memory.buffer, 0, pages*65536); | ||
buffer.set(copy(coredump)); | ||
|
||
library.setMemory(memory.buffer); | ||
library.setInput(" sample.tex \n\\end\n"); | ||
|
||
await WebAssembly.instantiate(code, { | ||
library: library, | ||
env: { memory: memory } | ||
}); | ||
|
||
const dvi = library.readFileSync("sample.dvi"); | ||
|
||
let html = ""; | ||
const page = new Writable({ | ||
write(chunk, _, callback) { | ||
html = html + chunk.toString(); | ||
callback(); | ||
} | ||
}); | ||
|
||
async function* streamBuffer() { | ||
yield Buffer.from(dvi); | ||
return; | ||
} | ||
const machine = await dvi2html(streamBuffer(), page); | ||
|
||
return { machine, html }; | ||
}; | ||
} | ||
|
||
function copy(src) { | ||
var dst = new Uint8Array(src.length); | ||
dst.set(src); | ||
return dst; | ||
} | ||
|
||
async function tex(input) { | ||
if (input.match('\\\\begin *{document}') === null) { | ||
input = '\\begin{document}\n' + input; | ||
} | ||
input = input + '\n\\end{document}\n'; | ||
|
||
library.deleteEverything(); | ||
library.writeFileSync( "sample.tex", Buffer.from(input) ); | ||
|
||
let memory = new WebAssembly.Memory({initial: pages, maximum: pages}); | ||
|
||
let buffer = new Uint8Array( memory.buffer, 0, pages*65536 ); | ||
buffer.set( copy(coredump) ); | ||
|
||
library.setMemory( memory.buffer ); | ||
library.setInput( " sample.tex \n\\end\n" ); | ||
|
||
let results = await WebAssembly.instantiate(code, { library: library, | ||
env: { memory: memory } | ||
}); | ||
|
||
return library.readFileSync( "sample.dvi" ); | ||
const dst = new Uint8Array(src.length); | ||
dst.set(src); | ||
return dst; | ||
} | ||
|
||
export async function TikZJax(root){ | ||
await load(); | ||
|
||
async function process(elt){ | ||
var text = elt.childNodes[0].nodeValue; | ||
|
||
var div = document.createElement('div'); | ||
|
||
let dvi = await tex(text); | ||
|
||
let html = ""; | ||
const page = new Writable({ | ||
write(chunk, _, callback) { | ||
html = html + chunk.toString(); | ||
callback(); | ||
} | ||
}); | ||
|
||
async function* streamBuffer() { | ||
yield Buffer.from(dvi); | ||
return; | ||
} | ||
|
||
let machine = await dvi2html( streamBuffer(), page ); | ||
div.style.display = 'flex'; | ||
div.style.width = machine.paperwidth.toString() + "pt"; | ||
div.style.height = machine.paperheight.toString() + "pt"; | ||
div.style['align-items'] = 'center'; | ||
div.style['justify-content'] = 'center'; | ||
|
||
div.innerHTML = html; | ||
let svg = div.getElementsByTagName('svg'); | ||
svg[0].setAttribute("width", machine.paperwidth.toString() + "pt"); | ||
svg[0].setAttribute("height", machine.paperheight.toString() + "pt"); | ||
svg[0].setAttribute("viewBox", `-72 -72 ${machine.paperwidth} ${machine.paperheight}`); | ||
|
||
elt.parentNode.replaceChild(div, elt); | ||
}; | ||
|
||
var scripts = root.getElementsByTagName('script'); | ||
var tikzScripts = Array.prototype.slice.call(scripts).filter( | ||
(e) => (e.getAttribute('type') === 'text/tikz')); | ||
|
||
tikzScripts.reduce( async (promise, element) => { | ||
await promise; | ||
return process(element); | ||
}, Promise.resolve()); | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,54 @@ | ||
import { TikZJax } from "./TikZJax"; | ||
import { load } from "./TikZJax"; | ||
|
||
// document.currentScript polyfill | ||
if (document.currentScript === undefined) { | ||
const scripts = document.getElementsByTagName("script"); | ||
document.currentScript = scripts[scripts.length - 1]; | ||
} | ||
const url = new URL(document.currentScript.src); | ||
|
||
// Determine where we were loaded from; we"ll use that to find a | ||
// tikzwolke server that can handle our POSTing tikz code | ||
// host includes the port | ||
const host = url.host; | ||
const urlRoot = url.protocol + "//" + host; | ||
|
||
window.TikZJax = async function TikZJax(root){ | ||
const tex = await load(urlRoot + "/tex.wasm", urlRoot + "/core.dump.gz"); | ||
|
||
async function process(elt){ | ||
const text = elt.childNodes[0].nodeValue; | ||
const { machine, html } = await tex(text); | ||
|
||
const div = document.createElement("div"); | ||
|
||
div.style.display = "flex"; | ||
div.style.width = machine.paperwidth.toString() + "pt"; | ||
div.style.height = machine.paperheight.toString() + "pt"; | ||
div.style["align-items"] = "center"; | ||
div.style["justify-content"] = "center"; | ||
|
||
div.innerHTML = html; | ||
const svg = div.getElementsByTagName("svg"); | ||
svg[0].setAttribute("width", machine.paperwidth.toString() + "pt"); | ||
svg[0].setAttribute("height", machine.paperheight.toString() + "pt"); | ||
svg[0].setAttribute("viewBox", `-72 -72 ${machine.paperwidth} ${machine.paperheight}`); | ||
|
||
elt.parentNode.replaceChild(div, elt); | ||
}; | ||
|
||
const scripts = root.getElementsByTagName("script"); | ||
const tikzScripts = Array.prototype.slice.call(scripts).filter( | ||
(e) => (e.getAttribute("type") === "text/tikz")); | ||
|
||
tikzScripts.reduce(async (promise, element) => { | ||
await promise; | ||
return process(element); | ||
}, Promise.resolve()); | ||
} | ||
|
||
window.LoadTikZJax = load; | ||
|
||
window.TikZJax = TikZJax; | ||
if (!window.TikZJaxNoAutostart) { | ||
window.onload = TikZJax(document); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is considered a bug or not, but |
||
} | ||
window.onload = () => TikZJax(document); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
load
returns a function that sets up wasm with the loaded payload/core dumps, so theoretically you could load different wasm/coredumps.The main point of this is to prevent loading multiple times though. I don't see any benefit to loading multiple times.