summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/html/output.rb
blob: 66ff0ba5da4ff342102fb766875ab5fcbfd8efab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module CodeRay
module Encoders
  class HTML
    module Output
      def self.wrap_string_in string, element, css = nil
        case element
        when :span
          SPAN
        when :div
          return string if string[/\A<(?:div|table)\b/]
          DIV
        when :page
          string = wrap_string_in(string, :div) unless string[/\A<(?:div|table)\b/]
          PAGE.sub('<%CSS%>', css ? css.stylesheet : '')
        else
          raise ArgumentError, 'Unknown wrap element: %p' % [element]
        end.sub('<%CONTENT%>', string)
      end
      
      def self.wrap! str, template, target
        target = Regexp.new(Regexp.escape("<%#{target}%>"))
        if template =~ target
          str[0,0] = $`
          str << $'
        else
          raise "Template target <%%%p%%> not found" % target
        end
      end
      
      SPAN = '<span class="CodeRay"><%CONTENT%></span>'
      
      DIV = <<-DIV
<div class="CodeRay">
  <div class="code"><pre><%CONTENT%></pre></div>
</div>
      DIV
      
      PAGE = <<-PAGE
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <style type="text/css">
.CodeRay .line-numbers a {
  text-decoration: inherit;
  color: inherit;
}
body {
  background-color: white;
  padding: 0;
  margin: 0;
}
<%CSS%>
.CodeRay {
  border: none;
}
  </style>
</head>
<body>

<%CONTENT%>
</body>
</html>
      PAGE
    end
  end
end
end