Friday, March 1, 2013

code2html wrapper for previewing different styles

In my effort to continue contributing to the web, I'm documenting coding projects or efforts that I think others may find useful.

At best, I'll teach something to someone they didn't know before. At worst, I'll have a place to document some of my code.

So, a little about this project. I tend to automate things that I use over and over. For example, making dynamic sql queries, or preparing column headers so that I can just paste them into the previous query I was using and do things with them.

Anyhow, this particular script helps me pick out the best formatted code2html entry.

Looks a little like this:

Google Chrome Output of code2html


... except it gives a breakdown of ALL the different code styles.

When I find the one I want, I hit right click in the general area "inspect element" , highligh, copy html, and drop it into blogger.

DONE.









 1 #!/bin/bash
 2 
 3 file=$1
 4 out=$(/bin/mktemp)
 5 browser="/usr/bin/chromium-browser"
 6 
 7 #is it running interactively, or receiving a pipe?
 8 if [ ! -t 0 ]; then
 9    echo "getting STDIN..."
10    file=$(/bin/mktemp)
11    cat - > ${file}
12 else
13    if [[ ! -f ${file} ]] ; then
14       echo "Bailing, invalid INPUT file"
15       rm -f ${out}
16       exit
17    fi
18 fi
19 
20 types=($(code2html -m | grep 'modes:' | sed 's/Defined modes: //;s/\.$//;s/, /\n/g'))
21 html=($(code2html -m | grep 'outputformats:' | sed 's/Defined outputformats: //;s/\.$//;s/, /\n/g'))
22 
23 function print_out {
24    echo "<html>"
25    echo "<body style='background-color:black;color:white;'>"
26    for i in ${types[@]}; do
27       echo -ne "<h1>==============================================<br>\n\n\n\n"
28       echo -ne "${i}\n\n\n\n"
29       echo -ne "<br>==============================================</h1>"
30       echo "<div id='${i}'>"
31       echo "<pre style='background-color:white;color:black;white-space:pre'>"
32       /usr/bin/code2html --replace-tabs 3 --language-mode ${i} --linenumbers  --no-header ${file} 
33       echo "</pre>"
34       echo "You can select, copy and paste the code below"
35       echo "<textarea style='width:100%;height:150px'>"
36       cat ${file}
37       #a little something special in case I run into any code that has the below tag with spaces
38       echo '</ t e x t a r e a >' | sed 's/ //g'
39 
40       echo "<br><br>"
41       echo -ne "\n\n\n\n"
42       echo "</div>"
43    done
44    echo "</body>"
45    echo "</html>"
46 }
47 echo source: ${file}
48 echo outfile: ${out}
49 
50 print_out > ${out}
51 ${browser} ${out} 
You can select, copy and paste the code below

No comments:

Post a Comment