Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmuellr/10425112 to your computer and use it in GitHub Desktop.
Save pmuellr/10425112 to your computer and use it in GitHub Desktop.
nice html from a specially prompted shell session
#!/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, '&amp;'
line = line.replace /</g, '&lt;'
line = line.replace />/g, '&gt;'
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