Add support for song titles.
[Akkordarbeit.git] / lib / akkordarbeit / html_formatter.rb
blob817f6fdcdfc8ea6354de6201c42c18c9a5a84e89
1 # vim: fileencoding=UTF-8 ft=ruby syn=ruby ts=2 sw=2 ai eol et si
3 # Copyright (c) 2009 Jörg W Mittag <mailto:JoergWMittag+Akkordarbeit@GoogleMail.Com>
4 # This code is licensed under the terms of the MIT License (see LICENSE.rdoc)
6 libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7 $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
9 require 'cgi'
11 module Akkordarbeit
12   class HtmlFormatter
13     def format(parsetree, title = 'Song-Sheet')
14       output =  <<-"HERE"
15 <!DOCTYPE html>
16 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
17         <head>
18                 <meta http-equiv='Content-type'     content='text/html; charset=UTF-8' />
19                 <title>#{escape(title)}</title>
20                 <meta http-equiv='content-language' content='en' />
21                 <style>
22                         p {
23                                 line-height: 300%;
24                                 max-width: 30em;
25                         }
26                         .chord {
27                                 position: relative;
28                         }
29                         .chord span {
30                                 position: absolute;
31                                 bottom: 40%;
32                                 font-size: 66%;
33                                 font-weight: bold;
34                         }
35                         .chord .brackets {
36                                 display: none;
37                         }
38                 </style>
39         </head>
40         <body>
41                 <header>
42                         <h1>#{escape(title)}</h1>
43                 </header>
44                 <section>
45       HERE
46       parsetree.each do |section|
47         output << "\t"*3 << "<p>\n"
48         section.each do |line|
49           output << "\t"*4
50           last_chord = nil
51           line.each do |token|
52             regex = /(?:\[(.*?)\])/
53             if regex.match(token)
54               last_chord = $1
55             else
56               unless last_chord
57                 output << escape(token)
58               else
59                 token = '&nbsp;' if token =~ /^\s$/
60                 output << "<span class='chord'><span><span class='brackets'>[</span>#{escape(last_chord)}<span class='brackets'>]</span></span>#{escape(token)}</span>"
61                 last_chord = nil
62               end
63             end
64           end
65           output << "<br />\n"
66         end
67         output.gsub! %r|<br />\Z|, ''
68         output << "\t"*3 << "</p>\n"
69       end
70       return output << <<-'HERE'
71                 </section>
72         </body>
73 </html>
74     HERE
75     end
77     private
79     def escape(str)
80       return CGI.escapeHTML(str)
81     end
82   end
83 end