Menu

[r4761]: / branches / parsnip / Src / Main / CS.SourceCode.Hiliter.Renderers.CSS.pas  Maximize  Restore  History

Download this file

201 lines (176 with data), 5.9 kB

  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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines a class that generates CSS code to enable syntax highlighted source
* to be displayed in HTML. CSS code uses a highlighter's attributes. Access to
* CSS class names is also provided.
}
unit CS.SourceCode.Hiliter.Renderers.CSS;
interface
uses
// Project
CS.SourceCode.Hiliter.Brushes,
CS.SourceCode.Hiliter.Themes,
UCSSBuilder;
// TODO: fix documentation comments
type
{
THiliterCSS:
Class generates CSS code to enable syntax highlighted source to be displayed
in HTML. CSS code uses a highlighter's attributes. Access to CSS class names
is also provided.
}
THiliterCSS = class(TObject)
strict private
var
fTheme: TSyntaxHiliteTheme;
{Highlighter for which CSS is to be generated}
procedure BuildAttrCSS(const BrushID, AttrID: string;
const CSSBuilder: TCSSBuilder);
{Builds CSS class for an attribute.
@param Elem [in] Highlighter element for which CSS is required.
@param CSSBuilder [in] Object used to build and store the CSS.
}
procedure BuildCommonThemeCSS(const CSSBuilder: TCSSBuilder);
public
constructor Create(const Theme: TSyntaxHiliteTheme);
{Class constructor. Sets up object ready to generate code for a syntax
highlighter.
@param HiliterAttrs [in] Attributes to be used in highlighter.
}
class function GetMainCSSClassName: string;
{Gets name of main CSS class used for all highlighted code.
@return Required class name.
}
class function GetElemCSSClassName(const BrushID, AttrID: string): string;
{Gets name of CSS class associated with a highlighter element.
@param Elem [in] Identifies element for which class name required.
@return Required class name.
}
procedure BuildBrushCSS(const ABrush: TSyntaxHiliterBrush;
const CSSBuilder: TCSSBuilder);
procedure BuildThemeCSS(const CSSBuilder: TCSSBuilder);
end;
implementation
uses
// Delphi
SysUtils,
Graphics,
// Project
UCSSUtils,
UStrUtils;
{ THiliterCSS }
procedure THiliterCSS.BuildAttrCSS(const BrushID, AttrID: string;
const CSSBuilder: TCSSBuilder);
var
AttrStyle: TSyntaxHiliteAttrStyle;
begin
AttrStyle := fTheme.GetStyle(BrushID, AttrID);
// We only create CSS class if element attribute's style is non-null
if fTheme.IsBaseStyle(AttrStyle) then
Exit;
with CSSBuilder.AddSelector('.' + GetElemCSSClassName(BrushID, AttrID)) do
begin
if AttrStyle.Background <> fTheme.DefaultBackground then
AddProperty(TCSS.BackgroundColorProp(AttrStyle.Background));
if AttrStyle.Foreground <> fTheme.DefaultForeground then
AddProperty(TCSS.ColorProp(AttrStyle.Foreground));
AddProperty(TCSS.FontWeightProp(AttrStyle.FontStyles));
AddProperty(TCSS.FontStyleProp(AttrStyle.FontStyles));
AddProperty(TCSS.TextDecorationProp(AttrStyle.FontStyles));
end;
end;
procedure THiliterCSS.BuildBrushCSS(const ABrush: TSyntaxHiliterBrush;
const CSSBuilder: TCSSBuilder);
var
Attrs: TArray<TSyntaxHiliterAttr>;
Attr: TSyntaxHiliterAttr;
begin
// Add font definition in main class
BuildCommonThemeCSS(CSSBuilder);
Attrs := ABrush.SupportedAttrs;
for Attr in Attrs do
BuildAttrCSS(ABrush.ID, Attr.ID, CSSBuilder);
end;
procedure THiliterCSS.BuildCommonThemeCSS(const CSSBuilder: TCSSBuilder);
begin
// Add font definition in main class
if CSSBuilder.Selectors['.' + GetMainCSSClassName] = nil then
begin
with CSSBuilder.AddSelector('.' + GetMainCSSClassName) do
begin
AddProperty(TCSS.FontFamilyProp(fTheme.FontName, cfgMonoSpace));
AddProperty(TCSS.FontSizeProp(fTheme.FontSize));
if fTheme.DefaultBackground <> clNone then
AddProperty(TCSS.BackgroundColorProp(fTheme.DefaultBackground));
if fTheme.DefaultForeground <> clNone then
AddProperty(TCSS.BackgroundColorProp(fTheme.DefaultForeground));
end;
end;
end;
procedure THiliterCSS.BuildThemeCSS(const CSSBuilder: TCSSBuilder);
var
BrushID: string;
Brush: TSyntaxHiliterBrush;
Attr: TSyntaxHiliterAttr;
begin
BuildCommonThemeCSS(CSSBuilder);
for BrushID in TSyntaxHiliterBrushes.SupportedBrushIDs do
begin
Brush := TSyntaxHiliterBrushes.CreateBrush(BrushID);
try
for Attr in Brush.SupportedAttrs do
BuildAttrCSS(Brush.ID, Attr.ID, CSSBuilder);
finally
Brush.Free;
end;
end;
end;
constructor THiliterCSS.Create(const Theme: TSyntaxHiliteTheme);
{Class constructor. Sets up object ready to generate code for a syntax
highlighter.
@param HiliterAttrs [in] Attributes to be used in highlighter.
}
begin
inherited Create;
Assert(Assigned(Theme), ClassName + '.Create: Theme is nil');
fTheme := Theme;
end;
class function THiliterCSS.GetElemCSSClassName(const BrushID, AttrID: string):
string;
{Gets name of CSS class associated with a highlighter element.
@param Elem [in] Identifies element for which class name required.
@return Required class name.
}
var
I: Integer;
begin
Result := StrToLower(BrushID + '-' + AttrID);
if Result = '-' then
Result := '_null_'
else
begin
if not CharInSet(Result[1], ['a'..'z', '_']) then
Result[1] := '_';
for I := 2 to Length(Result) do
begin
if not CharInSet(Result[I], ['a'..'z', '0'..'9', '-', '_']) then
Result[I] := '_';
end;
end;
end;
class function THiliterCSS.GetMainCSSClassName: string;
{Gets name of main CSS class used for all highlighted code.
@return Required class name.
}
begin
Result := 'highlighted-source';
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.