|
| 1 | +# |
| 2 | +# The MIT License (MIT) |
| 3 | +# |
| 4 | +# Copyright (c) 2024-2025 Kris Jusiak <[email protected]> |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | +# |
| 24 | +# Requirements: |
| 25 | +# - apt-get install coreutils # base64 |
| 26 | +# - apt-get install imagemagick # convert |
| 27 | +# - apt-get install libsixel-bin # sixel |
| 28 | +# - pip install ansi2html # ansi2html |
| 29 | +# |
| 30 | +# Usage: |
| 31 | +# ./app 2>&1 | ./export.sh markdown > app.md |
| 32 | +# ./app 2>&1 | ./export.sh notebook > app.ipynb |
| 33 | +# ./app 2>&1 | ./export.sh html > app.html |
| 34 | + |
| 35 | +function markdown() { |
| 36 | + rm -rf tmp |
| 37 | + mkdir tmp |
| 38 | + stdin=$(mktemp) |
| 39 | + stdout=$(mktemp) |
| 40 | + |
| 41 | + cat - > $stdin |
| 42 | + |
| 43 | + i=0 |
| 44 | + while grep -q $'\x1bP' $stdin; do |
| 45 | + sed -z 's/\(.*\)\x1bP.*/\1/' $stdin >> $stdout |
| 46 | + sed -z 's/.*\(\x1bP.*\x1b\\\).*/\1/' $stdin | \ |
| 47 | + convert sixel:- png:- > tmp/$((i++)).png |
| 48 | + sed -z 's/.*\x1b\\\(.*\)/\1/' $stdin >> $stdout |
| 49 | + mv $stdout $stdin |
| 50 | + done |
| 51 | + |
| 52 | + cat $stdin > tmp/$((i++)) |
| 53 | +} |
| 54 | + |
| 55 | +function html() { |
| 56 | + stdin=$(mktemp) |
| 57 | + stdout=$(mktemp) |
| 58 | + |
| 59 | + cat - | ansi2html > $stdin |
| 60 | + |
| 61 | + while grep -q $'\x1bP' $stdin; do |
| 62 | + sed -z 's/\(.*\)\x1bP.*/\1/' $stdin >> $stdout |
| 63 | + sed -z 's/.*\(\x1bP.*\x1b\\\).*/\1/' $stdin | \ |
| 64 | + convert sixel:- png:- | base64 | tr -d '\n' | \ |
| 65 | + xargs -i{} echo "<img src='data:image/png;base64,{}'/><br />" >> $stdout |
| 66 | + sed -z 's/.*\x1b\\\(.*\)/\1/' $stdin >> $stdout |
| 67 | + mv $stdout $stdin |
| 68 | + done |
| 69 | + cat $stdin |
| 70 | +} |
| 71 | + |
| 72 | +function notebook() { |
| 73 | + cat <<EOF |
| 74 | + { |
| 75 | + "cells": [ |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + $(cat - | markdown | sed 's/.*/"&\\n",/') |
| 81 | + "" |
| 82 | + ] |
| 83 | + } |
| 84 | + ], |
| 85 | + "metadata": { |
| 86 | + "jupytext": { |
| 87 | + "cell_metadata_filter": "-all", |
| 88 | + "main_language": "python", |
| 89 | + "notebook_metadata_filter": "-all" |
| 90 | + } |
| 91 | + }, |
| 92 | + "nbformat": 4, |
| 93 | + "nbformat_minor": 5 |
| 94 | + } |
| 95 | +EOF |
| 96 | +} |
| 97 | + |
| 98 | +if [ $# -lt 1 ]; then |
| 99 | + echo "Usage: $0 <markdown|notebook|html> # reads from stdin" |
| 100 | + exit 1 |
| 101 | +fi |
| 102 | + |
| 103 | +cat - | $1 |
0 commit comments