Class: Syntax::Convertors::EmbeddedStylesHtml

Inherits:
Abstract
  • Object
show all
Defined in:
lib/embedded_styles_html.rb

Overview

Converts code to HTML, but embeds format information in span style attributes for those cases where you have no control over the CSS in the header. Wordpress, I’m looking at you…

Instance Method Summary collapse

Instance Method Details

#convert(text, pre = true) ⇒ Object

Converts the given text to HTML, using spans to represent token groups of any type but :normal (which is always unhighlighted). If pre is true, the html is automatically wrapped in pre tags. Style values are looked up in a hash and embedded directly.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/embedded_styles_html.rb', line 69

def convert( text, pre=true )
  html = ""
  html << "<pre>" if pre
  regions = []
  @tokenizer.tokenize( text ) do |tok|
    value = html_escape(tok)
    case tok.instruction
      when :region_close then
        regions.pop
        html << "</span>"
      when :region_open then
        regions.push tok.group
        html << "<span style=\"#{group_colors[tok.group]}\">#{value}"
      else
        if tok.group == ( regions.last || :normal )
          html << value
        else
          html << "<span style=\"#{group_colors[tok.group]}\">#{value}</span>"
        end
    end
  end
  html << "</span>" while regions.pop
  html << "</pre>" if pre
  html
end

#group_colorsObject

Returns a Hash containing styles rules for each token group

Gonna try and recreate this: pre

background-color: #f1f1f3;
color: #112;
padding: 10px;
font-size: 1.1em;
overflow: auto;
margin: 4px 0px;
      width: 95%;

/* Syntax highlighting */ pre .normal {} pre .comment { color: #005; font-style: italic; } pre .keyword { color: #A00; font-weight: bold; } pre .method { color: #077; } pre .class { color: #074; } pre .module { color: #050; } pre .punct { color: #447; font-weight: bold; }

pre .symbol { color: #099; } pre .string { color: #944; background: #FFE; } pre .char { color: #F07; } pre .ident { color: #004; } pre .constant { color: #07F; } pre .regex { color: #B66; background: #FEF; } pre .number { color: #F99; } pre .attribute { color: #5bb; } pre .global { color: #7FB; } pre .expr { color: #227; } pre .escape { color: #277; }



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/embedded_styles_html.rb', line 44

def group_colors
  { :normal     => '',
    :comment    => 'color: #005; font-style: italic;',
    :keyword    => 'color: #A00; font-weight: bold;',
    :method     => 'color: #077',
    :class      => 'color: #074',
    :module     => 'color: #050',
    :punct      => 'color: #447; font-weight: bold;',
    :symbol     => 'color: #099',
    :string     => 'color: #944; background: #FFE;',
    :char       => 'color: #F07;',
    :ident      => 'color: #004;',
    :constant   => 'color: #07F;',
    :regex      => 'color: #B66; background: #FEF;',
    :number     => 'color: #F99;',
    :attribute  => 'color: #5bb;',
    :global     => 'color: #7FB;',
    :expr       => 'color: #227;',
    :escape     => 'color: #277'}
end