Add a simple HTML formatter.
[Akkordarbeit.git] / lib / akkordarbeit / html_formatter.rb
blob4ea7fa9c0badd74749462b16e4de6d1a3d889d83
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 module Akkordarbeit
10   class HtmlFormatter
11     def format(parsetree)
12       output =  <<-'HERE'
13 <!DOCTYPE html>
14 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
15         <head>
16                 <meta http-equiv='Content-type'     content='text/html; charset=UTF-8' />
17                 <title>Song-Sheet</title>
18                 <meta http-equiv='content-language' content='en' />
19                 <style>
20                         p {
21                                 line-height: 300%;
22                                 max-width: 30em;
23                         }
24                         .chord {
25                                 position: relative;
26                         }
27                         .chord span {
28                                 position: absolute;
29                                 bottom: 40%;
30                                 font-size: 66%;
31                                 font-weight: bold;
32                         }
33                         .chord .brackets {
34                                 display: none;
35                         }
36                 </style>
37         </head>
38         <body>
39                 <header>
40                         <h1>Song-Sheet</h1>
41                 </header>
42                 <section>
43       HERE
44       parsetree.each do |section|
45         output << "\t"*3 << "<p>\n"
46         section.each do |line|
47           output << "\t"*4
48           last_chord = nil
49           line.each do |token|
50             regex = /(?:\[(.*?)\])/
51             if regex.match(token)
52               last_chord = $1
53             else
54               unless last_chord
55                 output << token
56               else
57                 token = '&nbsp;' if token =~ /^\s$/
58                 output << "<span class='chord'><span><span class='brackets'>[</span>#{last_chord}<span class='brackets'>]</span></span>#{token}</span>"
59                 last_chord = nil
60               end
61             end
62           end
63           output << "<br />\n"
64         end
65         output << "\t"*3 << "</p>\n"
66       end
67       return output << <<-'HERE'
68                 </section>
69         </body>
70 </html>
71     HERE
72     end
73   end
74 end