monochromatic

monochromatic blog: http://blog.z3bra.org
git clone git://z3bra.org/monochromatic
Log | Files | Refs

tomarkdown.sed (1346B)


      1 #!/bin/sed -f
      2 
      3 # remove header
      4 1,/<div id='wrapper'>/d
      5 
      6 # no more indentation
      7 s/^\s*//
      8 
      9 # reformat titles
     10 /<h[1-6][^>]*>/{
     11     $!N
     12     s/<h1[^>]*>\s*\(.*\)\(<\/h1>\)\?/# \1/
     13     s/<h2[^>]*>\s*\(.*\)\(<\/h2>\)\?/## \1/
     14     s/<h3[^>]*>\s*\(.*\)\(<\/h3>\)\?/### \1/
     15     s/<h4[^>]*>\s*\(.*\)\(<\/h4>\)\?/#### \1/
     16     s/<h5[^>]*>\s*\(.*\)\(<\/h5>\)\?/##### \1/
     17     s/<h6[^>]*>\s*\(.*\)\(<\/h6>\)\?/###### \1/
     18 }
     19 
     20 # remove closing title tags
     21 /^\s*<\/h[1-6]>\s*$/d
     22 s/<\/h[1-6]>//
     23 
     24 # replace all links/strong/emphasis
     25 s/<a href=["']\(.*\)["']>\(.*\)<\/a>/[\2](\1)/
     26 s/<\/\?strong>/**/g
     27 s/<\/\?em>/_/g
     28 
     29 # lists
     30 /<ul[^>]*>/,/<\/ul>/{
     31     /^\s*<\/\?ul>$/d
     32     s/<li>\(.*\)<\/li>/* \1/
     33 }
     34 
     35 # images
     36 /<a class='a_img'/{
     37     $!N
     38     /<img/{
     39         $!N
     40         s/<a class='[a-z_]*' href='\(.*\)'> *\n*\
     41              *<img class='[a-z_]*' src='\(.*\)' alt='\(.*\)'\/> *\n *<\/a>/[![\3](\2)](\1)/
     42     }
     43 }
     44 
     45 # pre formatted text
     46 /<pre>/,/<\/pre>/{
     47     s/<\/\?code>//g
     48     s/<\/\?pre>//g
     49     s/^/    /
     50 }
     51 
     52 /<code>/{
     53     $!N
     54     s/<\/\?code>/`/g
     55 }
     56 
     57 # new lines
     58 s/<br[\/ ]*>/  /
     59 
     60 # paragraphs
     61 /<p[^>]*>/,/<\/p>/s/<\/\?p>//g
     62 
     63 # quotes
     64 /<blockquote[^>]*>/,/<\/blockquote>/s/^\s*/> /
     65 s/<\/\?blockquote>//g
     66 /^> $/d
     67 
     68 # remove simple tag, and span tags
     69 #s/<\/\?[A-Za-z0-9]*>//g
     70 s/<\/\?span[^>]*>//g
     71 s/<\/\?section[^>]*>//g
     72 s/<\/\?article[^>]*>//g
     73 
     74 # remove footer
     75 /<\/div>/,$d