mirror of
https://github.com/Pomax/BezierInfo-2.git
synced 2025-09-02 21:02:49 +02:00
up to and including section 21
This commit is contained in:
@@ -1,35 +1,32 @@
|
||||
var sha1 = require("sha1");
|
||||
var fs = require("fs");
|
||||
var execSync = require("child_process").execSync;
|
||||
var blockLoader = require("block-loader");
|
||||
|
||||
var op = "\\[";
|
||||
var ed = "\\]";
|
||||
var options = {
|
||||
start: "\\[",
|
||||
end: "\\]",
|
||||
|
||||
/**
|
||||
* Is there going to be anything to convert here?
|
||||
*/
|
||||
function hasLaTeX(input) {
|
||||
return input.indexOf(op) !== -1 && input.indexOf(ed) !== -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* We look for MathJax/KaTeX style data, and make sure
|
||||
* it is escaped properly so that JSX conversion will
|
||||
* still work.
|
||||
*/
|
||||
function escapeBlockLaTeX(source) {
|
||||
// we can't do this with regexp, unfortunately.
|
||||
var from = 0, curr, term;
|
||||
var newsource = "", latex;
|
||||
for(curr = source.indexOf(op, from); curr !== -1; from = term + ed.length, curr = source.indexOf(op, from)) {
|
||||
newsource += source.substring(from, curr);
|
||||
term = source.indexOf(ed, from);
|
||||
if(term === -1) {
|
||||
// that's a problem...
|
||||
throw new Error("improperly closed LaTeX encountered!");
|
||||
preprocessors: [
|
||||
/**
|
||||
* convert some convenience colour markers to true LaTeX form.
|
||||
*/
|
||||
function colorPreProcess(input) {
|
||||
var regexp = new RegExp("([A-Z]+)\\[([^\\]]+)\\]",'g');
|
||||
var output = input.replace(regexp, function(_,color,content) {
|
||||
if(content.indexOf(" ")!==-1) { content = " " + content; }
|
||||
return "{\\color{"+color.toLowerCase()+"}"+content.replace(/ /g,"\\ ")+"}";
|
||||
});
|
||||
return output;
|
||||
}
|
||||
latex = source.substring(curr, term + ed.length);
|
||||
],
|
||||
|
||||
/**
|
||||
* We look for MathJax/KaTeX style data, and make sure
|
||||
* it is escaped properly so that JSX conversion will
|
||||
* still work.
|
||||
*/
|
||||
process: function escapeBlockLaTeX(latex) {
|
||||
// convert this LaTeX code into an SVG file in ./images/latex,
|
||||
// using mathjax-node in the ./tools directory.
|
||||
var hash = sha1(latex);
|
||||
@@ -53,29 +50,9 @@ function escapeBlockLaTeX(source) {
|
||||
var ex2rem = 0.45;
|
||||
var w = parseFloat(svg.match(/width="([^"]+)"/)[1]) * ex2rem;
|
||||
var h = parseFloat(svg.match(/height="([^"]+)"/)[1]) * ex2rem;
|
||||
var rewrite = `<img className="LaTeX SVG" src="${filename}" style={{width:"${w}rem", height:"${h}rem"}} />`;
|
||||
|
||||
newsource += rewrite;
|
||||
return `<img className="LaTeX SVG" src="${filename}" style={{width:"${w}rem", height:"${h}rem"}} />`;
|
||||
}
|
||||
return newsource + source.substring(from);
|
||||
}
|
||||
|
||||
/**
|
||||
* ...
|
||||
*/
|
||||
function colorPreProcess(input) {
|
||||
var regexp = new RegExp("([A-Z]+)\\[([^\\]]+)\\]",'g');
|
||||
var output = input.replace(regexp, function(_,color,content) {
|
||||
if(content.indexOf(" ")!==-1) { content = " " + content; }
|
||||
return "{\\color{"+color.toLowerCase()+"}"+content.replace(/ /g,"\\ ")+"}";
|
||||
});
|
||||
return output;
|
||||
};
|
||||
|
||||
module.exports = function(source) {
|
||||
this.cacheable();
|
||||
if (!hasLaTeX(source)) return source;
|
||||
return escapeBlockLaTeX(colorPreProcess(source));
|
||||
};
|
||||
|
||||
module.exports = blockLoader(options);
|
||||
|
||||
|
@@ -1,4 +0,0 @@
|
||||
/**
|
||||
* because somethings you just need a no-op
|
||||
*/
|
||||
module.exports = function noop() {};
|
@@ -1,40 +1,19 @@
|
||||
/**
|
||||
* <pre> should preserve newlines. Damnit, JSX
|
||||
*/
|
||||
var blockLoader = require("block-loader");
|
||||
var options = {
|
||||
start: "<pre>",
|
||||
end: "</pre>",
|
||||
|
||||
var op = "<pre>";
|
||||
var ed = "</pre>";
|
||||
|
||||
function hasTokens(input) {
|
||||
return input.indexOf(op) !== -1 && input.indexOf(ed) !== -1;
|
||||
}
|
||||
|
||||
function fixPreBlocks(source) {
|
||||
// we can't do this with regexp, unfortunately.
|
||||
var from = 0, curr, term;
|
||||
var newsource = "", pre;
|
||||
for(curr = source.indexOf(op, from); curr !== -1; from = term + ed.length, curr = source.indexOf(op, from)) {
|
||||
newsource += source.substring(from, curr);
|
||||
term = source.indexOf(ed, from);
|
||||
if(term === -1) {
|
||||
// that's a problem...
|
||||
throw new Error("improperly closed LaTeX encountered!");
|
||||
}
|
||||
pre = source.substring(curr+op.length, term);
|
||||
pre = pre.replace(/&/g,'&')
|
||||
.replace(/</g,'<')
|
||||
.replace(/>/g,'>')
|
||||
.replace(/([{}])/g,"{'$1'}")
|
||||
.replace(/\n/g,"{'\\n'}");
|
||||
newsource += "<pre>" + pre + "</pre>";
|
||||
/**
|
||||
* There's a fair few things we'll want to safify for
|
||||
* <pre> elements used in JSX...
|
||||
*/
|
||||
process: function fixPreBlocks(pre) {
|
||||
return pre
|
||||
.replace(/&/g,'&')
|
||||
.replace(/</g,'<')
|
||||
.replace(/>/g,'>')
|
||||
.replace(/([{}])/g,"{'$1'}")
|
||||
.replace(/\n/g,"{'\\n'}");
|
||||
}
|
||||
return newsource + source.substring(from);
|
||||
}
|
||||
|
||||
|
||||
module.exports = function(source) {
|
||||
this.cacheable();
|
||||
if (!hasTokens(source)) return source;
|
||||
return fixPreBlocks(source);
|
||||
};
|
||||
|
||||
module.exports = blockLoader(options);
|
||||
|
Reference in New Issue
Block a user