Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function plutostateToJulia(statefile, juliafile) {
const fakeBind = `macro bind(def, element)
quote
local el = \$(esc(element))
global \$(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end`;
const _notebook_header = '### A Pluto.jl notebook ###';
// We use a creative delimiter to avoid accidental use in code
// so don't get inspired to suddenly use these in your code!
const _cell_id_delimiter = '# ╔═╡ ';
const _order_delimiter = '# ╠═';
const _order_delimiter_folded = '# ╟─';
const _cell_suffix = '\n\n';
return new Promise(async (resolve, reject) => {
const state = await parsePlutostate(statefile);
const cellOrder = state['cell_order'];
const cellExecutionOrder = state['cell_execution_order'];
const cellsDict = Object.fromEntries(Object.entries(state['cell_inputs']).map(([k, v]) => ([k, {
cellId: k,
code: v.code,
codeFolded: v.code_folded
}])));
const notebookId = uuid.v4();
let content = '';
content += _notebook_header + '\n';
content += '# 0.14.5\n'; // TODO: Update to dynamic (latest) version
content += '\n';
content += 'using Markdown\n';
content += 'using InteractiveUtils\n';
// Bind detection
if(Object.values(cellsDict).filter(c => c.code.includes('@bind')).length > 0) {
content += '\n';
content += '# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following \'mock version\' of @bind gives bound variables a default value (instead of an error).\n';
content += fakeBind + '\n';
}
content += '\n';
for(let cellId of cellExecutionOrder) {
const cell = cellsDict[cellId];
content += _cell_id_delimiter + cellId + '\n';
content += cell.code.replace(_cell_id_delimiter, '# ');
content += _cell_suffix;
}
content += _cell_id_delimiter + 'Cell order:\n';
for(let cellId of cellOrder) {
const cell = cellsDict[cellId];
const delim = cell.codeFolded ? _order_delimiter_folded : _order_delimiter
content += delim + cellId + '\n';
}
fs.writeFile(juliafile, content, err => {
if(err) reject(err);
else {
resolve();
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment