|
| 1 | +## 2021-02-26 |
| 2 | +*Author: @JensLincke* |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +## Some #ScriptEvolution, Example: #PDF Outline Extraction |
| 7 | + |
| 8 | +A little script that produces the outline of a paper using some regular expressions. |
| 9 | + |
| 10 | +### First Version |
| 11 | + |
| 12 | +Requires Halo interaction to "select" the right parent element in the PDF. |
| 13 | + |
| 14 | +```javascript |
| 15 | +that.querySelectorAll("div") |
| 16 | + .map(ea => ea.textContent) |
| 17 | + .filter(ea => ea.match(/^\s*[0-9][0-9\.]*\s+[A-Z]/)) |
| 18 | + .join("\n") |
| 19 | +``` |
| 20 | + |
| 21 | +### Second Version |
| 22 | + |
| 23 | +Does not require Halo interaction, since it searches the PDF itself. |
| 24 | + |
| 25 | +```javascript |
| 26 | +var pdf = lively.findAllElements(ea => ea.localName == "lively-pdf", true)[0] |
| 27 | +pdf.get("#container").querySelectorAll("div") |
| 28 | + .map(ea => ea.textContent) |
| 29 | + .filter(ea => ea.match(/^\s*[0-9][0-9\.]*\s+[A-Z]/)) |
| 30 | + .join("\n") |
| 31 | +``` |
| 32 | + |
| 33 | +### Third Version |
| 34 | + |
| 35 | +Moved into the [PDF component](edit://src/components/widgets/lively-pdf.js) |
| 36 | + |
| 37 | +```javascript |
| 38 | +export default class LivelyPDF extends Morph { |
| 39 | + initialize() { |
| 40 | + // ... |
| 41 | + this.addEventListener('contextmenu', evt => this.onContextMenu(evt), false); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + onContextMenu(evt) { |
| 46 | + if (!evt.shiftKey) { |
| 47 | + evt.stopPropagation(); |
| 48 | + evt.preventDefault(); |
| 49 | + |
| 50 | + var menu = new ContextMenu(this, [ |
| 51 | + ["show outline", async () => { |
| 52 | + var workspace = await lively.openWorkspace(this.extractOutline()) |
| 53 | + workspace.parentElement.setAttribute("title","Outline") |
| 54 | + workspace.mode = "text" |
| 55 | + }]]); |
| 56 | + menu.openIn(document.body, evt, this); |
| 57 | + return true; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + extractOutline() { |
| 62 | + return this.get("#container").querySelectorAll("div") |
| 63 | + .map(ea => ea.textContent) |
| 64 | + .filter(ea => ea.match(/^\s*[0-9][0-9\.]*\s+[A-Z]/)) |
| 65 | + .join("\n") |
| 66 | + } |
| 67 | + // ... |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +On ChoRyu_2014_JavascriptModuleSystemExploringTheDesignSpace_.pdf this produces: |
| 72 | + |
| 73 | +``` |
| 74 | +1. Introduction |
| 75 | +2014 ACM 978-1-4503-2772-5/14/04. . . $15.00. |
| 76 | +2. Design Issues in the JavaScript Module System |
| 77 | +2.1 Import and Export Declarations |
| 78 | +2.2 Name Conflict Resolution |
| 79 | +2.3 Prototypes of Module Instance Objects |
| 80 | +3. Formalization of the JavaScript Module System |
| 81 | +3.1 JavaScript Module System |
| 82 | +3.2 Formal Specification of the Design Issues |
| 83 | +3.3 Validity Properties of the JavaScript Module System |
| 84 | +4. Implementation of the JavaScript Module System |
| 85 | +5. Related Work |
| 86 | +6. Conclusion |
| 87 | +262 Edition 6). |
| 88 | +``` |
| 89 | + |
| 90 | +It contains some garbage but since I can copy it into my notes it is very useful. |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +### Future Version |
| 95 | + |
| 96 | +This script uses patterns to find the outline, but shouldn't there be some PDF build in outlines? |
| 97 | + |
0 commit comments