Skip to content
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

(doc): Suggest a python variable for including double braces in markdown output #1641

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ define([
'require',
'notebook/js/cell',
'base/js/security',
'components/marked/lib/marked',
'nbextensions/python-markdown/marked.min',
'base/js/events',
'notebook/js/textcell'
], function(IPython, $, requirejs, cell, security, marked, events, textcell) {
Expand All @@ -27,19 +27,20 @@ define([
* @method execute_python
* @param cell {Cell} notebook cell
* @param text {String} text in cell
* @returns {String} interpolated cell content
*/
var execute_python = function(cell,text) {
/* never execute code in untrusted notebooks */
if (IPython.notebook.trusted === false ) {
return undefined
}
/* always clear stored variables if notebook is dirty */
if (IPython.notebook.dirty === true ) delete cell.metadata.variables;
// search for code in double curly braces: {{}}
var found = false;
var newtext = text.replace(/{{(.*?)}}/g, function(match,tag,cha) {
found = true;
if (tag === "") return undefined;
/* never execute code in untrusted notebooks */
if (IPython.notebook.trusted === false ) {
return "Untrusted notebook. Activate trust or disable python-markdown extension";
}
if (tag === "") return "Error: empty double braces {{}}!";
var code = tag;
var id = 'python_'+cell.cell_id+'_'+cha; /* create an individual ID */
var thiscell = cell;
Expand All @@ -60,9 +61,9 @@ define([
var html;
if (out_data.msg_type === "error") {
var text = "**" + out_data.content.ename + "**: " + out_data.content.evalue;
html = marked(text);
html = marked.marked(text);
} else if (out_data.msg_type === "stream") {
html = marked(out_data.content.text);
html = marked.marked(out_data.content.text);
var t = html.match(/^\s*<p>([\s\S]*?)<\/p>\s*$/); //strip <p> and </p> that marked (maybe) adds and we don't want
html = t !== null ? t[1] : html;
var q = html.match(/^&#39;([\s\S]*?)&#39;$/); // strip quotes from strings
Expand All @@ -84,11 +85,11 @@ define([
var png = ul['image/png'];
html = '<img src="data:image/png;base64,' + png + '"/>';
} else if (ul['text/markdown'] != undefined) {
html = marked(ul['text/markdown']);
html = marked.marked(ul['text/markdown']);
} else if (ul['text/html'] != undefined) {
html = ul['text/html'];
} else {
html = marked(ul['text/plain']);
html = marked.marked(ul['text/plain']);
// [\s\S] is used to also catch newlines
var t = html.match(/^\s*<p>([\s\S]*?)<\/p>\s*$/); //strip <p> and </p> that marked adds and we don't want
html = t !== null ? t[1] : html;
Expand All @@ -108,7 +109,7 @@ define([
cell.notebook.kernel.execute(code, callbacks, {silent: false, store_history : false, stop_on_error: false });
return "<span id='"+id+"'></span>"; // add HTML tag with ID where output will be placed
}
return undefined;
return "No kernel for cell";
} else {
/* Notebook not dirty: replace tags with metadata */
val = cell.metadata.variables[tag];
Expand All @@ -129,7 +130,7 @@ define([
if (text !== undefined) {
element[0].innerHTML = text;
MathJax.Hub.Queue(["Typeset",MathJax.Hub,element[0]]);
}
} /* else the {{}} markers were not found, skip actions */
};

/* force rendering of markdown cell if notebook is dirty */
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Link: readme.md
Icon: python-markdown.png
Main: main.js
Preprocessor: pymdpreprocessor.py
Compatibility: 4.x, 5.x
Compatibility: 4.x, 5.x, 6.x
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This is indicated by the "trusted" check mark:

![trusted](trusted.png)

If you see the "unstrusted" question mark, use File->Trust Notebook in the menu.
If you see the "untrusted" question mark, use File->Trust Notebook in the menu.

**Caution: If you trust a notebook, you allow it to execute any code that is contained between the `{{...}}`
curly braces on your notebook server.**
Expand All @@ -51,6 +51,17 @@ is trusted by looking at the check mark at the top of the window.
**Caution:** There is no restriction in the expression you can embedd between the curly braces `{{ }}`.
Be careful as you might crash your browser if you return too large datasets, or worse.

Escaping
--------

To generate content including double braces, '{{', you can define a python string, eg:

```python
double_left_curly_brace = '{{'
```

Place the string variable as `{{double_left_curly_brace}}` in your markdown cell.


Exporting
---------
Expand Down