-
Notifications
You must be signed in to change notification settings - Fork 14
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
Render json with latex #107
Changes from 11 commits
2235fec
004e3f8
9c0a5b1
3c71f79
f321708
c1c278c
353a3cc
ad2080a
3191fed
f934ae6
a98b1a7
e56fe4d
8364b3f
8b6e4e8
1503b97
ccaa9c9
17151fd
a565a74
6cbfd2d
6ca50dc
17e3a48
a3ae7bf
d7a3a85
67c3bc8
bffc348
d2eca23
73289ca
605a616
b9e6046
5d9fec0
73b27a2
64e65cc
92fa43f
5e1abdc
359a267
330f191
e4b1a91
e18ccd1
755a902
3d7bb17
373d156
9a7c24b
0b7e9e1
dfe466e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const {renderMarkdown} = require("./renderMarkdown"); | ||
const { renderJSON, renderMarkdown } = require("./renderer.js"); | ||
const process = require("process"); | ||
const Handlebars = require("handlebars"); | ||
const shell = require("shelljs"); | ||
|
@@ -30,13 +30,17 @@ class Task extends Unit { | |
basedir, | ||
source, | ||
target, | ||
js_modules, | ||
css_modules, | ||
lu | ||
) { | ||
super(unit_type, label, exp_path, basedir); | ||
this.lu = lu; | ||
this.content_type = validContentType(content_type); | ||
this.source = source; | ||
this.target = target; | ||
this.js_modules = js_modules || []; | ||
this.css_modules = css_modules || []; | ||
} | ||
|
||
static unit_type = UnitTypes.TASK; | ||
|
@@ -50,6 +54,8 @@ class Task extends Unit { | |
t["basedir"], | ||
t["source"], | ||
t["target"], | ||
t["js_modules"], | ||
t["css_modules"], | ||
lu | ||
); | ||
} | ||
|
@@ -102,6 +108,51 @@ class Task extends Unit { | |
); | ||
} | ||
|
||
isURL(source) { | ||
try { | ||
new URL(source); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
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. Please log a statement here mentioning that ${source} is not a valid URL. Will help in case an intended URL has errors in it 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. A log statement is added |
||
} | ||
} | ||
|
||
jsPath() { | ||
let final_paths = []; | ||
for (let js_path of this.js_modules) { | ||
if (this.isURL(js_path)) { | ||
final_paths.push(js_path); | ||
continue; | ||
} | ||
|
||
const absolute_path = path.resolve( | ||
path.join(Config.build_path(this.exp_path), this.basedir, js_path) | ||
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. What happens if the path.resolve returns an error, in case the path mentioned is not a valid path in the system? 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. path.resolve will return a relative path to that file, after that, we use fs.existsSync to check whether the file actually exists or not. |
||
); | ||
final_paths.push( | ||
path.relative(path.dirname(this.targetPath()), absolute_path) | ||
); | ||
} | ||
return final_paths; | ||
} | ||
|
||
cssPath() { | ||
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. Both jsPath and cssPath can be the same function with final_path and js_path/css_path passed in as parameters. 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. A new function is created by the name finalPath that takes 1 parameter |
||
let final_paths = []; | ||
for (let css_path of this.css_modules) { | ||
if (this.isURL(css_path)) { | ||
final_paths.push(css_path); | ||
continue; | ||
} | ||
|
||
const absolute_path = path.resolve( | ||
path.join(Config.build_path(this.exp_path), this.basedir, css_path) | ||
); | ||
final_paths.push( | ||
path.relative(path.dirname(this.targetPath()), absolute_path) | ||
); | ||
} | ||
return final_paths; | ||
} | ||
|
||
buildPage(exp_info, lab_data, options) { | ||
let assets_path = path.relative( | ||
path.dirname(this.targetPath()), | ||
|
@@ -134,6 +185,8 @@ class Task extends Unit { | |
isSimulation: false, | ||
isAssesment: false, | ||
assets_path: assets_path, | ||
js_modules: this.jsPath(), | ||
css_modules: this.cssPath(), | ||
lab_data: lab_data, | ||
exp_info: exp_info, | ||
lab: lab_data.lab, | ||
|
@@ -200,18 +253,19 @@ class Task extends Unit { | |
page_data.isAssesment = true; | ||
|
||
if (shell.test("-f", this.sourcePath())) { | ||
page_data.questions = require(this.sourcePath()); | ||
if (page_data.questions.version) { | ||
let JSONdata = require(this.sourcePath()); | ||
JSONdata = renderJSON(JSON.stringify(JSONdata)); | ||
if (JSONdata.version) { | ||
/** | ||
* The below condition will only work if the version in the json is either 2 or 2.0, for any update in version | ||
* it needs to be changed here accordingly | ||
*/ | ||
if (page_data.questions.version == 2) { | ||
page_data.isJsonVersion2 = page_data.questions.version; | ||
if (JSONdata.version == 2) { | ||
page_data.isJsonVersion2 = JSONdata.version; | ||
} | ||
page_data.questions = page_data.questions.questions; | ||
page_data.questions = JSON.parse(JSONdata).questions; | ||
} | ||
page_data.questions_str = JSON.stringify(page_data.questions); | ||
page_data.questions_str = JSONdata; | ||
page_data.isJsonVersion = true; | ||
} else { | ||
const jsonpath = this.sourcePath(); | ||
|
@@ -233,6 +287,7 @@ class Task extends Unit { | |
|
||
const page_template = fs.readFileSync( | ||
path.resolve( | ||
__dirname, | ||
Config.Experiment.ui_template_name, | ||
"pages", | ||
"content.handlebars" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,103 @@ | ||
exp.v1.js | ||
const path = require("path"); | ||
const Handlebars = require("handlebars"); | ||
const shell = require("shelljs"); | ||
|
||
const { Experiment } = require("./Experiment.js"); | ||
const Config = require("./Config.js"); | ||
const { BuildEnvs, validBuildEnv } = require("./Enums.js"); | ||
|
||
function run(src, lab_data, build_options) { | ||
// if the experiment repo does not contain experiment descriptor we will add the default descriptor. | ||
if (!shell.test("-f", Experiment.descriptorPath(src))) { | ||
shell.cp( | ||
path.resolve(Config.Experiment.default_descriptor), | ||
path.resolve(this.src, Experiment.descriptorPath(src)) | ||
); | ||
} | ||
|
||
const exp = new Experiment(src); | ||
exp.init(Handlebars); | ||
// Validation | ||
if(build_options.isValidate) | ||
exp.validate(build_options); | ||
|
||
// if the experiment repo contains contributors.md file we will add its lu to the descriptor. | ||
if (shell.test("-f", Experiment.contributorsPath(src))) { | ||
if(shell.head({'-n': 1}, Experiment.contributorsPath(src)).includes('EMPTY')) | ||
{ | ||
console.log('Contributors.md file is empty, please add contributors to the file.'); | ||
} | ||
else | ||
{ | ||
exp.includeContributors(); | ||
} | ||
} | ||
exp.includeFeedback(); | ||
exp.build(Handlebars, lab_data, build_options); | ||
} | ||
|
||
module.exports.run = run; | ||
module.exports.build_experiment = run; | ||
|
||
if (require.main === module) { | ||
const args = require("minimist")(process.argv.slice(2)); | ||
|
||
// for backwards compatibility if the env is not given assume it to | ||
// be testing. | ||
const build_options = {}; | ||
|
||
if (args.env) { | ||
build_options.env = validBuildEnv(args.env); | ||
} else { | ||
build_options.env = BuildEnvs.TESTING; | ||
} | ||
|
||
// if the path is not provided assume "../" for backward | ||
// compatability. | ||
|
||
let src = "../"; | ||
|
||
if (args._.length === 1) { | ||
src = path.resolve(args._[0]); | ||
} | ||
|
||
/* | ||
We are making an assumption here that if you are running this | ||
script from the command line then this is being used for testing | ||
the individual experiment, and by convention when we are testing | ||
individual experiment, we do not use any lab level information and | ||
we do not include analytics. | ||
|
||
So, while it is possible to give build_options.env as | ||
'production', it does not make any sense and we should probably | ||
remove it or change the build process to make it useful. | ||
|
||
Anyways, for now, we will send an empty object as lab_data and | ||
hope things work out. | ||
*/ | ||
const default_lab_data = {}; | ||
|
||
const paths = path.resolve(src).split(path.sep); | ||
const base = paths[paths.length - 1]; | ||
|
||
// Get the experiment name and developer institute name from the repo name of the | ||
// format exp-<expName>-<devInstituteName> e.g. exp-geometry-optimization-molecules-iiith | ||
const path_name_regex = /exp-(?<expName>[\w-]+)-(?<devInstituteName>\w+)$/i; | ||
const match = base.match(path_name_regex); | ||
|
||
if (match && match.groups) { | ||
default_lab_data.exp_short_name = match.groups.expName; | ||
default_lab_data.collegeName = match.groups.devInstituteName.toUpperCase(); | ||
default_lab_data.phase = "Testing"; | ||
default_lab_data.lab = "Virtual Lab"; | ||
default_lab_data.lab_display_name = "Virtual Lab Display Name"; | ||
default_lab_data.broadArea = { name: "Test" }; | ||
} else { | ||
console.log("No match found"); | ||
} | ||
|
||
run(src, default_lab_data, build_options); | ||
} | ||
// node exp.v1.js --env=production ../ | ||
// node exp.v1.js --env=testing ../ | ||
// node exp.v1.js --env=local ../ |
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.
Now that we are panning to have an asset directory, maybe we can put the KaTeX assets inside /asset/katex directory
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.
We have moved katex_assets to the assets directory