Last active
August 29, 2015 13:58
-
-
Save pmuellr/10425112 to your computer and use it in GitHub Desktop.
nice html from a specially prompted shell session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env coffee | |
#------------------------------------------------------------------------------- | |
# if you set your prompt thusly: | |
# | |
# PS1="\w \$ " # add an extra \n at the beginning, maybe? | |
# | |
# you can capture the text from your terminal session, including the | |
# prompts, and then filter with this program (pass to stdin), which | |
# will produce stand-alone HTML for the terminal session, with the | |
# prompt and command highlighted. | |
# | |
# On the mac, after pasting the terminal session into the clipboard, | |
# you can use the following command to create the .html file: | |
# | |
# pbpaste | console-ize.coffee > console.html | |
# | |
#------------------------------------------------------------------------------- | |
main = -> | |
readStdin() | |
#------------------------------------------------------------------------------- | |
genHTML = (input) -> | |
console.log """ | |
<style> | |
.console u { | |
background: #DEF; | |
margin: -0.1em -0.5em; | |
padding: 0.1em 0.5em; | |
border: thin solid #777; | |
border-radius: 0.5em; | |
text-decoration: none; | |
} | |
.console b { | |
background: #CFC; | |
margin: -0.1em -0.5em; | |
padding: 0.1em 0.5em; | |
border: thin solid #777; | |
border-radius: 0.5em; | |
} | |
</style> | |
<pre class="console"> | |
""" | |
for line in input.split '\n' | |
line = line.replace /&/g, '&' | |
line = line.replace /</g, '<' | |
line = line.replace />/g, '>' | |
match = line.match /^(~.*? \$ )(.*)$/ | |
if match | |
line = "<b>#{match[1]} <u>#{match[2]}</u></b>" | |
console.log line | |
console.log "</pre>" | |
#------------------------------------------------------------------------------- | |
readStdin = -> | |
process.stdin.setEncoding 'utf8' | |
stdin = "" | |
process.stdin.on 'readable', -> | |
chunk = process.stdin.read() | |
return unless chunk? | |
stdin += chunk | |
process.stdin.on 'error', (err) -> | |
console.error "error reading stdin: #{err}" | |
process.exit 1 | |
process.stdin.on 'end', -> | |
genHTML stdin | |
#------------------------------------------------------------------------------- | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment