jupyter-ijavascript-utils 1.59.0 → 1.60.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/DOCS.md CHANGED
@@ -74,6 +74,7 @@ Give it a try here:
74
74
  [![Binder:what can I do with this](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/paulroth3d/jupyter-ijavascript-utils/main?labpath=example.ipynb)
75
75
 
76
76
  ## What's New
77
+ * 1.60 - Make post-processing of documents easier with {@link module:ijs.markDocumentPosition|ijs.markDocumentPosition}
77
78
  * 1.59 -
78
79
  * #95 - give control with page breaks. So we can render text before the page break (like for headers) - or even get the html used and render it how we want. (ex: {@link module:ijs.generatePageBreakStylesHTML|ijs.printPageBreak})
79
80
  * #96 - Support {@link module:ijs.internalComment|ijs.internalComment} in notebooks (meaning it can render in markdown, but can be disabled when preparing to print)
package/Dockerfile CHANGED
@@ -1,3 +1,3 @@
1
1
  # syntax=docker/dockerfile:1
2
2
 
3
- FROM darkbluestudios/jupyter-ijavascript-utils:binder_1.59.0
3
+ FROM darkbluestudios/jupyter-ijavascript-utils:binder_1.60.0
package/README.md CHANGED
@@ -54,6 +54,7 @@ This is not intended to be the only way to accomplish many of these tasks, and a
54
54
  ![Screenshot of example notebook](docResources/img/mainExampleNotebook.png)
55
55
 
56
56
  # What's New
57
+ * 1.60 - Make post-processing of documents easier with ijs.utils.markDocumentPosition
57
58
  * 1.59 -
58
59
  * #95 - give control with page breaks. So we can render text before the page break (like for headers) - or even get the html used and render it how we want.
59
60
  * #96 - Support internal comments in notebooks (meaning it can render in markdown, but can be disabled when preparing to print)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jupyter-ijavascript-utils",
3
- "version": "1.59.0",
3
+ "version": "1.60.0",
4
4
  "description": "Utilities for working with iJavaScript - a Jupyter Kernel",
5
5
  "homepage": "https://jupyter-ijavascript-utils.onrender.com/",
6
6
  "license": "MIT",
@@ -23,7 +23,8 @@
23
23
  "doc": "npm run prep:docdash && node_modules/.bin/jsdoc -c ./jsdoc.json -u ./tutorials ./DOCS.md",
24
24
  "prep:docdash": "cp docResources/docdash/layout.tmpl node_modules/docdash/tmpl/layout.tmpl && rm -rf docResources/notebooks/node_modules",
25
25
  "open:coverage": "open ./coverage/lcov-report/index.html",
26
- "open:docs": "open ./docs/index.html"
26
+ "open:docs": "open ./docs/index.html",
27
+ "open:binder": "open https://mybinder.org/v2/gh/paulroth3d/jupyter-ijavascript-utils/main"
27
28
  },
28
29
  "repository": {
29
30
  "type": "git",
package/src/ijs.js CHANGED
@@ -29,11 +29,15 @@ require('./_types/global');
29
29
  * * {@link module:ijs.clearOutput|ijs.clearOutput} - clears the output to declutter results (like importing libraries, or functions)
30
30
  * * {@link module:ijs.initializePageBreaks|ijs.initializePageBreaks} - call at least once to allow pageBreaks when rendering PDFs
31
31
  * * {@link module:ijs.printPageBreak|ijs.printPageBreak} - call to print a page break when rendering PDFs
32
- * * {@link module:ijs.generatePageBreakStylesHTML|ijs.generatePageBreakStylesHTML} - generates the html used in to allow for pagebreaks
32
+ * * {@link module:ijs.generatePageBreakStylesHTML|ijs.printPageBreak} - generates the html used in to allow for pagebreaks
33
33
  * (so you can render them as you'd like)
34
34
  * * {@link module:ijs.generatePageBreakHTML|ijs.generatePageBreakHTML} - generates the html that uses the styles to render pagebreaks
35
35
  * (so you can render them as you'd like)
36
36
  * * {@link module:ijs.internalComment|ijs.internalComment} - render markdown, but be able to turn it off, prior to printing
37
+ * * {@link module:ijs.markDocumentPosition|ijs.markDocumentPosition} - render something in html for post processing
38
+ * (such as identifying the start of document and removing everything prior)
39
+ * * {@link module:ijs.markStartOfContent|ijs.markStartOfContent} - most comment mark - the start of content
40
+ * * {@link module:ijs.markEndOfContent|ijs.markEndOfContent} - most comment mark - the end of content
37
41
  * * using a cache for long running executions
38
42
  * * {@link module:ijs.useCache|ijs.useCache()} - perform an expensive calculation and write to a cache, or read from the cache transparently
39
43
  *
@@ -273,10 +277,12 @@ module.exports.markdown = function markdown(markdownText, display) {
273
277
  * @param {Jupyter$$} [display] - the display to render the output to.
274
278
  */
275
279
  module.exports.internalComment = function internalComment(shouldRender, markdownText, display) {
276
- if (!shouldRender) {
277
- IJSUtils.clearOutput();
280
+ if (!IJSUtils.detectIJS()) return;
281
+ const displayToUse = display || global.$$;
282
+ if (shouldRender) {
283
+ displayToUse.markdown(markdownText, display);
278
284
  } else {
279
- IJSUtils.markdown(markdownText, display);
285
+ displayToUse.html('<span class="output-to-be-removed-from-printing">&nbsp;</span>');
280
286
  }
281
287
  };
282
288
 
@@ -626,7 +632,7 @@ module.exports.htmlScript = function htmlScripts(
626
632
  * (This is useful to put after importing libraries,
627
633
  * or defining a list of functions)
628
634
  */
629
- module.exports.clearOutput = function clearOutput(outputText = '') {
635
+ module.exports.clearOutput = function clearOutput(outputText = '', outputHTML = '') {
630
636
  //-- you must be in iJavaScript container to rendeer
631
637
  const context = IJSUtils.detectContext();
632
638
 
@@ -634,10 +640,119 @@ module.exports.clearOutput = function clearOutput(outputText = '') {
634
640
  return;
635
641
  }
636
642
 
637
- context.$$.text(outputText);
643
+ if (!outputHTML) {
644
+ context.$$.text(outputText);
645
+ } else {
646
+ context.$$.html(outputHTML);
647
+ }
638
648
  };
639
649
  module.exports.noOutputNeeded = module.exports.clearOutput;
640
650
 
651
+ /**
652
+ * Marks the start of content.
653
+ *
654
+ * This could be useful for removing all cells and content prior from printing
655
+ * when reviewing the html output.
656
+ *
657
+ * @see {@link module:ijs.markDocumentPosition|ijs.markDocumentPosition}
658
+ * @example
659
+ * utils.ijs.markStartOfContent();
660
+ *
661
+ * //-- will render the following in the cell
662
+ * // <!-- ##Start of content. This could be used to remove everything prior from printing.## -->
663
+ * // <span id="ijsutils-start-of-content">&nbsp;</span>
664
+ */
665
+ module.exports.markStartOfContent = function markStartOfContent() {
666
+ IJSUtils.markDocumentPosition('ijsutils-start-of-content', 'Start of content. This could be used to remove everything prior from printing.');
667
+ };
668
+
669
+ /**
670
+ * Marks the end of content.
671
+ *
672
+ * This could be useful for removing all cells and content after this point from printing
673
+ * when reviewing the html output.
674
+ *
675
+ * @see {@link module:ijs.markDocumentPosition|ijs.markDocumentPosition}
676
+ * @example
677
+ * utils.ijs.markEndOfContent();
678
+ *
679
+ * //-- will render the following in the cell
680
+ * // <!-- ##End of content. This could be used to remove everything after from printing.## -->
681
+ * // <span id="ijsutils-end-of-content">&nbsp;</span>
682
+ */
683
+ module.exports.markEndOfContent = function markEndOfContent() {
684
+ IJSUtils.markDocumentPosition('ijsutils-end-of-content', 'End of content. This could be used to remove everything after from printing.');
685
+ };
686
+
687
+ /**
688
+ * Renders HTML that can be easily found in the HTML of an exported document.
689
+ *
690
+ * This allows for simpler and consistent post-processing
691
+ * (such as which cells to remove prior to rendering to PDF)
692
+ *
693
+ * @param {String} elementId - id of the element that could be found through document.querySelector
694
+ * @param {String} [markerComment] - Any additional text to put in an html comment
695
+ * @param {String} [innerHTML] - Optional text to be shown in the span (useful for making it easier to find)
696
+ * @returns {String} the html
697
+ * @private
698
+ */
699
+ module.exports.generatePositionMarkerHTML = function generatePositionMarkerHTML(elementId, markerComment, innerHTML) {
700
+ const cleanInnerHTML = innerHTML || '&nbsp;';
701
+ const html = `
702
+ ${!markerComment ? '' : `<!-- ##${markerComment}## -->`}
703
+ <span id="${elementId}">${cleanInnerHTML}</span>
704
+ `;
705
+ return html;
706
+ };
707
+
708
+ /**
709
+ * Renders HTML that can be easily found in the HTML of an exported document.
710
+ *
711
+ * This allows for simpler and consistent post-processing
712
+ * (such as which cells to remove prior to rendering to PDF)
713
+ *
714
+ * For example, within a Jupyter / Jupyter Lab Notebook:
715
+ *
716
+ * ```
717
+ * utils.ijs.markDocumentPosition('ijsutils-start-of-content', 'Remove this and everything above', '<h1>Start of content</h1>');
718
+ *
719
+ * //-- this will render a cell with the following:
720
+ * // <!-- ##start of stuff## -->
721
+ * // <span id="ijsutils-start-of-content"><h1>Start of content</h1></span>
722
+ * ```
723
+ *
724
+ * We can then look for that id within the output of the notebook.<br />
725
+ * (Like within a bookmarklet)
726
+ *
727
+ * ```
728
+ * parentCell = document.querySelector('#ijsutils-start-of-content').closest('.jp-Cell');
729
+ * ```
730
+ *
731
+ * and then remove it - and any cells before it
732
+ *
733
+ * ```
734
+ * cellsToRemove = [];
735
+ * while(parentCell) {
736
+ * cellsToRemove.push(parentCell);
737
+ * parentCell = parentCell.previousElementSibling;
738
+ * }
739
+ *
740
+ * //-- to remove them
741
+ * cellsToRemove.forEach((el) => el.remove())
742
+ * ```
743
+ *
744
+ * @param {String} elementId - id of the element that could be found through document.querySelector
745
+ * @param {String} [markerComment] - Any additional text to put in an html comment
746
+ * @param {String} [innerHTML] - Optional text to be shown in the span (useful for making it easier to find)
747
+ * @see {@link module:ijs.markStartOfContent|ijs.markStartOfContent}
748
+ * @see {@link module:ijs.markEndOfContent|ijs.markEndOfContent}
749
+ */
750
+ module.exports.markDocumentPosition = function markDocumentPosition(elementId, markerComment, innerHTML) {
751
+ const resultHTML = IJSUtils.generatePositionMarkerHTML(elementId, markerComment, innerHTML);
752
+
753
+ IJSUtils.clearOutput(null, resultHTML);
754
+ };
755
+
641
756
  /**
642
757
  * Returns the HTML used for generating pageBreaks within the library.
643
758
  *