lang/python/ SimpleMarkdownHelper
Example usage:
cat file | md q
pp | md q # pp is a command I have which pastes from the clipboard on cygwin, macos and x11.
takes a text file (or from the clipboard) adds leading >
to it and prints it out.
There is also the w command which textwraps. And commands are chained. So e.g.
cat file | md w q
first text-wraps the input, then blockquotes it. By doing
pp | md w q | pc
I can paste through a pipeline with md w q
and send the result back to the clipboard.
Source
#!/usr/bin/env python3
import sys
def main():
args = sys.argv[1:]
md = sys.stdin.read()
for arg in args:
if arg in actions:
md = actions[arg](md)
print(md)
def blockquote(md):
a = []
for x in md.splitlines():
a.append(f"> {x}")
return "\n".join(a)
import textwrap
def wrap(md):
t = textwrap.wrap(md)
return "\n".join(t)
actions = {}
actions['blockquote'] = blockquote
actions['q'] = blockquote
actions['w'] = wrap
if __name__ == "__main__":
main()
Source of pp and pc
#!/bin/dash
# pc -- copy to clipboard
if [ -n "$DISPLAY" ]; then # X11
cat "$@" | xsel -i -b
elif [ -d "/Applications" ]; then # macos
cat "$@" | pbcopy
elif [ -d "/cygdrive/c/cygwin64" ]; then # cygwin
cat "$@" > /dev/clipboard
else
echo "Cannot copy as not gui" > /dev/stderr
fi
#!/bin/dash
# pp - paste
if [ -n "$DISPLAY" ]; then # X11
paste() { xsel -o -b; }
elif [ -d "/Applications" ]; then # macos
paste() { pbpaste; }
elif [ -d "/cygdrive/c/cygwin64" ]; then # cygwin
paste() { cat /dev/clipboard; }
else
echo "Cannot paste as not gui" > /dev/stderr
fi
if [ -n "$1" ]; then
paste | tee "$1"
else
paste
fi