jupyter-ijavascript-utils 1.58.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,10 @@ 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}
78
+ * 1.59 -
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})
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)
77
81
  * 1.58 -
78
82
  * #91 - add {@link module:object.splitIntoDatums|splitIntoDatums(collection, fieldsToSplitBy)} - to make working with datum libraries - like vega-lite - easier
79
83
  * #92 - make using the cache easier for {@link module:file.useCache|file.useCache()} - as you can say to use the cache, and it will still work if the cache file does not exist yet.
package/Dockerfile CHANGED
@@ -1,3 +1,3 @@
1
1
  # syntax=docker/dockerfile:1
2
2
 
3
- FROM darkbluestudios/jupyter-ijavascript-utils:binder_1.58.0
3
+ FROM darkbluestudios/jupyter-ijavascript-utils:binder_1.60.0
package/README.md CHANGED
@@ -54,6 +54,10 @@ 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
58
+ * 1.59 -
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.
60
+ * #96 - Support internal comments in notebooks (meaning it can render in markdown, but can be disabled when preparing to print)
57
61
  * 1.58 -
58
62
  * #91 - add object.splitIntoDatums - to make working with datum libraries - like vega-lite - easier
59
63
  * #92 - make using the cache easier - as you can say to use the cache, and it will still work if the cache file does not exist yet.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jupyter-ijavascript-utils",
3
- "version": "1.58.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,6 +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.printPageBreak} - generates the html used in to allow for pagebreaks
33
+ * (so you can render them as you'd like)
34
+ * * {@link module:ijs.generatePageBreakHTML|ijs.generatePageBreakHTML} - generates the html that uses the styles to render pagebreaks
35
+ * (so you can render them as you'd like)
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
32
41
  * * using a cache for long running executions
33
42
  * * {@link module:ijs.useCache|ijs.useCache()} - perform an expensive calculation and write to a cache, or read from the cache transparently
34
43
  *
@@ -235,6 +244,8 @@ module.exports.detectIJS = function detectIJS() {
235
244
  * ![Screenshot of markdown](img/ijsMarkdown.png)
236
245
  *
237
246
  * @param {String} markdownText - The markdown to be rendered
247
+ * @param {*} markdownText - the markdown text to render
248
+ * @param {Jupyter$$} [display] - the display to render the output to.
238
249
  * @example
239
250
  *
240
251
  * utils.ijs.markdown(`# Overview
@@ -246,6 +257,35 @@ module.exports.markdown = function markdown(markdownText, display) {
246
257
  displayToUse.mime({ 'text/markdown': markdownText });
247
258
  };
248
259
 
260
+ /**
261
+ * Capture internal comments that can render as markdown (including images)
262
+ * but be turned off - so they are not rendered in the final output
263
+ *
264
+ * ```
265
+ * utils.ijs.internalComment(true, `![Screenshot](localhost://path-to-file)`);
266
+ * ```
267
+ *
268
+ * and then when preparing to ship, you don't include it
269
+ *
270
+ * ```
271
+ * printable=true
272
+ * utils.ijs.internalComment(!printable, `![Screenshot](localhost://path-to-file)`);
273
+ * ```
274
+ *
275
+ * @param {Boolean} shouldRender - whether the comment should be rendered to the output
276
+ * @param {String} markdownText - the markdown text to render
277
+ * @param {Jupyter$$} [display] - the display to render the output to.
278
+ */
279
+ module.exports.internalComment = function internalComment(shouldRender, markdownText, display) {
280
+ if (!IJSUtils.detectIJS()) return;
281
+ const displayToUse = display || global.$$;
282
+ if (shouldRender) {
283
+ displayToUse.markdown(markdownText, display);
284
+ } else {
285
+ displayToUse.html('<span class="output-to-be-removed-from-printing">&nbsp;</span>');
286
+ }
287
+ };
288
+
249
289
  /**
250
290
  * List the globals currently defined.
251
291
  *
@@ -592,7 +632,7 @@ module.exports.htmlScript = function htmlScripts(
592
632
  * (This is useful to put after importing libraries,
593
633
  * or defining a list of functions)
594
634
  */
595
- module.exports.clearOutput = function clearOutput(outputText = '') {
635
+ module.exports.clearOutput = function clearOutput(outputText = '', outputHTML = '') {
596
636
  //-- you must be in iJavaScript container to rendeer
597
637
  const context = IJSUtils.detectContext();
598
638
 
@@ -600,10 +640,149 @@ module.exports.clearOutput = function clearOutput(outputText = '') {
600
640
  return;
601
641
  }
602
642
 
603
- context.$$.text(outputText);
643
+ if (!outputHTML) {
644
+ context.$$.text(outputText);
645
+ } else {
646
+ context.$$.html(outputHTML);
647
+ }
604
648
  };
605
649
  module.exports.noOutputNeeded = module.exports.clearOutput;
606
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
+
756
+ /**
757
+ * Returns the HTML used for generating pageBreaks within the library.
758
+ *
759
+ * This gives you control over rendering the text together
760
+ *
761
+ * ```
762
+ * utils.ijs.generatePageBreakStylesHTML();
763
+ * // <style>
764
+ *
765
+ * //-- an identifier that can be used to find if this script exists on the page
766
+ * \/\* ID:___InitializePageBreaks___ \*\/
767
+ *
768
+ * // @media print {
769
+ * // .pagebreak { page-break-before: always; }
770
+ * // }
771
+ *
772
+ * // </style>
773
+ * ```
774
+ *
775
+ * @returns {String} - the html styles tag used to allow for page breaks to be generated
776
+ */
777
+ module.exports.generatePageBreakStylesHTML = function generatePageBreakStylesHTML() {
778
+ return `<style>
779
+ /* ID:___InitializePageBreaks___ */
780
+ @media print {
781
+ .pagebreak { page-break-before: always; } /* page-break-after works, as well */
782
+ }
783
+ </style>`;
784
+ };
785
+
607
786
  /**
608
787
  * Required to be called first - in order to write page-breaks in the html results.
609
788
  *
@@ -634,8 +813,26 @@ module.exports.noOutputNeeded = module.exports.clearOutput;
634
813
  *
635
814
  * * end of document
636
815
  * ```
816
+ *
817
+ * Note, sometimes you want to include a text prior to the page break,
818
+ * like mentioning that a page is left intentionally blank - or to identify
819
+ * this is the cell that has the style - so it shouldn't be removed prior to printing.
820
+ *
821
+ * ```
822
+ * utils.ijs.initializePageBreaks('<h1>This page left intentionally blank</h1>');
823
+ * ```
824
+ *
825
+ * Or, perhaps you always want a page break after defining the styles
826
+ *
827
+ * ```
828
+ * utils.ijs.initializePageBreaks(null, utils.ijs.generatePageBreakHTML());
829
+ * ```
830
+ *
831
+ * @param {String} [htmlToInjectBefore] - optional html text to include prior to the pageBreak
832
+ * (This can be helpful like - page left intentionally blank)
833
+ * @param {String} [htmlToInjectAfter] - optional html text to include prior to the pageBreak
637
834
  */
638
- module.exports.initializePageBreaks = function initializePageBreaks() {
835
+ module.exports.initializePageBreaks = function initializePageBreaks(htmlToInjectBefore, htmlToInjectAfter) {
639
836
  //-- you must be in iJavaScript container to rendeer
640
837
  const context = IJSUtils.detectContext();
641
838
 
@@ -643,20 +840,50 @@ module.exports.initializePageBreaks = function initializePageBreaks() {
643
840
  return;
644
841
  }
645
842
 
646
- context.$$.html(`<style>
647
- @media print {
648
- .pagebreak { page-break-before: always; } /* page-break-after works, as well */
649
- }
650
- </style>
651
- <div class="pagebreak"></div>
652
- `);
843
+ const cleanInjectionPrior = htmlToInjectBefore || '';
844
+ const htmlToRender = IJSUtils.generatePageBreakStylesHTML();
845
+ const cleanInjectionAfter = htmlToInjectAfter || '';
846
+
847
+ context.$$.html(`
848
+ ${cleanInjectionPrior}
849
+ ${htmlToRender}
850
+ ${cleanInjectionAfter}`);
851
+ };
852
+
853
+ /**
854
+ * Generates the html used for creating a page break used by the library.
855
+ *
856
+ * This gives you options about when and how to render it.
857
+ *
858
+ * ```
859
+ * utils.ijs.generatePageBreakHTML();
860
+ * //-- uses the style defined in initializePageBreaks
861
+ * // <div class="pagebreak"></div>
862
+ * ```
863
+ *
864
+ * For example, you can use this so you automatically have a page break after generating the styles
865
+ *
866
+ * ```
867
+ * utils.ijs.initializePageBreaks(null, utils.ijs.generatePageBreakHTML());
868
+ * ```
869
+ *
870
+ * @returns {String}
871
+ * @see {@link module:ijs.printPageBreak|ijs.printPageBreak}
872
+ * @see {@link module:ijs.initializePageBreaks|ijs.initializePageBreaks}
873
+ */
874
+ module.exports.generatePageBreakHTML = function generatePageBreakHTML() {
875
+ return '<div class="pagebreak"></div>';
653
876
  };
654
877
 
655
878
  /**
656
879
  * After the {@see module:ijs.initializePageBreaks|utils.ijs.initializePageBreaks} is called,
657
880
  * this will create another page break.
881
+ *
882
+ * @param {String} [htmlToInjectBefore] - optional html text to include prior to the pageBreak
883
+ * (This can be helpful like - page left intentionally blank)
884
+ * @param {String} [htmlToInjectAfter] - optional html text to include prior to the pageBreak
658
885
  */
659
- module.exports.printPageBreak = function printPageBreak() {
886
+ module.exports.printPageBreak = function printPageBreak(htmlToInjectBefore, htmlToInjectAfter) {
660
887
  //-- you must be in iJavaScript container to rendeer
661
888
  const context = IJSUtils.detectContext();
662
889
 
@@ -664,7 +891,9 @@ module.exports.printPageBreak = function printPageBreak() {
664
891
  return;
665
892
  }
666
893
 
667
- context.$$.html('<div class="pagebreak"></div>');
894
+ const htmlToRender = IJSUtils.generatePageBreakHTML();
895
+
896
+ context.$$.html(`${htmlToInjectBefore || ''}${htmlToRender}${htmlToInjectAfter || ''}`);
668
897
  };
669
898
 
670
899
  /**