Skip to content

Commit 005acb2

Browse files
committed
Merge branch 'feature/153-save-snippet-info-as-html' into develop
Fixes #153
2 parents baaf28e + b4efc1b commit 005acb2

8 files changed

+775
-115
lines changed

Src/ActiveText.UHTMLRenderer.pas

+11-5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ TCSSStyles = class(TObject)
6565
fTagInfoMap: TTagInfoMap;
6666
fIsStartOfTextLine: Boolean;
6767
fLINestingDepth: Cardinal;
68+
fTagGen: THTMLClass;
6869
const
6970
IndentMult = 2;
7071
procedure InitialiseTagInfoMap;
@@ -73,7 +74,7 @@ TCSSStyles = class(TObject)
7374
function MakeOpeningTag(const Elem: IActiveTextActionElem): string;
7475
function MakeClosingTag(const Elem: IActiveTextActionElem): string;
7576
public
76-
constructor Create;
77+
constructor Create(const ATagGenerator: THTMLClass = nil);
7778
destructor Destroy; override;
7879
function Render(ActiveText: IActiveText): string;
7980
end;
@@ -87,13 +88,18 @@ implementation
8788

8889
{ TActiveTextHTML }
8990

90-
constructor TActiveTextHTML.Create;
91+
constructor TActiveTextHTML.Create(const ATagGenerator: THTMLClass);
9192
begin
9293
inherited Create;
9394
fCSSStyles := TCSSStyles.Create;
9495
fBuilder := TStringBuilder.Create;
9596
fLINestingDepth := 0;
9697
InitialiseTagInfoMap;
98+
if not Assigned(ATagGenerator) then
99+
// default behaviour before ATagGenerator parameter was added
100+
fTagGen := TXHTML
101+
else
102+
fTagGen := ATagGenerator;
97103
end;
98104

99105
destructor TActiveTextHTML.Destroy;
@@ -145,7 +151,7 @@ procedure TActiveTextHTML.InitialiseTagInfoMap;
145151
function TActiveTextHTML.MakeClosingTag(const Elem: IActiveTextActionElem):
146152
string;
147153
begin
148-
Result := TXHTML.ClosingTag(fTagInfoMap[Elem.Kind].Name);
154+
Result := fTagGen.ClosingTag(fTagInfoMap[Elem.Kind].Name);
149155
end;
150156

151157
function TActiveTextHTML.MakeOpeningTag(const Elem: IActiveTextActionElem):
@@ -160,7 +166,7 @@ function TActiveTextHTML.MakeOpeningTag(const Elem: IActiveTextActionElem):
160166
Attrs := THTMLAttributes.Create;
161167
Attrs.Add('class', fCSSStyles.ElemClasses[Elem.Kind])
162168
end;
163-
Result := TXHTML.OpeningTag(fTagInfoMap[Elem.Kind].Name, Attrs);
169+
Result := fTagGen.OpeningTag(fTagInfoMap[Elem.Kind].Name, Attrs);
164170
end;
165171

166172
function TActiveTextHTML.Render(ActiveText: IActiveText): string;
@@ -242,7 +248,7 @@ function TActiveTextHTML.RenderText(const TextElem: IActiveTextTextElem):
242248
end
243249
else
244250
Result := '';
245-
Result := Result + TXHTML.Entities(TextElem.Text);
251+
Result := Result + fTagGen.Entities(TextElem.Text);
246252
end;
247253

248254
{ TActiveTextHTML.TCSSStyles }

Src/CodeSnip.dpr

+2-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ uses
376376
ClassHelpers.UGraphics in 'ClassHelpers.UGraphics.pas',
377377
ClassHelpers.UActions in 'ClassHelpers.UActions.pas',
378378
USaveInfoMgr in 'USaveInfoMgr.pas',
379-
ClassHelpers.RichEdit in 'ClassHelpers.RichEdit.pas';
379+
ClassHelpers.RichEdit in 'ClassHelpers.RichEdit.pas',
380+
UHTMLSnippetDoc in 'UHTMLSnippetDoc.pas';
380381

381382
// Include resources
382383
{$Resource ExternalObj.tlb} // Type library file

Src/CodeSnip.dproj

+1
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@
583583
<DCCReference Include="ClassHelpers.UActions.pas"/>
584584
<DCCReference Include="USaveInfoMgr.pas"/>
585585
<DCCReference Include="ClassHelpers.RichEdit.pas"/>
586+
<DCCReference Include="UHTMLSnippetDoc.pas"/>
586587
<None Include="CodeSnip.todo"/>
587588
<BuildConfiguration Include="Base">
588589
<Key>Base</Key>

Src/UCSSBuilder.pas

+19-10
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ TCSSBuilder = class(TObject)
7777
// Class that maps CSS selector names to selector objects
7878
TCSSSelectorMap = TObjectDictionary<string,TCSSSelector>;
7979
var
80-
fSelectors: TCSSSelectorMap; // Maps selector names to selector objects
80+
fSelectors: TCSSSelectorMap; // Maps selector names to selector objects
81+
fSelectorNames: TList<string>; // Lists selector names in order created
8182
function GetSelector(const Selector: string): TCSSSelector;
8283
{Read access method for Selectors property. Returns selector object with
8384
given name.
@@ -105,10 +106,13 @@ TCSSBuilder = class(TObject)
105106
procedure Clear;
106107
{Clears all selectors from style sheet and frees selector objects.
107108
}
109+
110+
/// <summary>Generates CSS code representing the style sheet.</summary>
111+
/// <returns><c>string</c>. The required CSS.</returns>
112+
/// <remarks>The selectors are returned in the order they were created.
113+
/// </remarks>
108114
function AsString: string;
109-
{Generates CSS code representing the style sheet.
110-
@return Required CSS code.
111-
}
115+
112116
property Selectors[const Selector: string]: TCSSSelector
113117
read GetSelector;
114118
{Array of CSS selectors in style sheet, indexed by selector name}
@@ -189,26 +193,29 @@ function TCSSBuilder.AddSelector(const Selector: string): TCSSSelector;
189193
begin
190194
Result := TCSSSelector.Create(Selector);
191195
fSelectors.Add(Selector, Result);
196+
fSelectorNames.Add(Selector);
192197
end;
193198

194199
function TCSSBuilder.AsString: string;
195-
{Generates CSS code representing the style sheet.
196-
@return Required CSS code.
197-
}
198200
var
201+
SelectorName: string; // name of each selector
199202
Selector: TCSSSelector; // reference to each selector in map
200203
begin
201204
Result := '';
202-
for Selector in fSelectors.Values do
205+
for SelectorName in fSelectorNames do
206+
begin
207+
Selector := fSelectors[SelectorName];
203208
if not Selector.IsEmpty then
204209
Result := Result + Selector.AsString;
210+
end;
205211
end;
206212

207213
procedure TCSSBuilder.Clear;
208214
{Clears all selectors from style sheet and frees selector objects.
209215
}
210216
begin
211-
fSelectors.Clear; // frees selector objects in .Values[]
217+
fSelectorNames.Clear;
218+
fSelectors.Clear; // frees owened selector objects in dictionary
212219
end;
213220

214221
constructor TCSSBuilder.Create;
@@ -221,13 +228,15 @@ constructor TCSSBuilder.Create;
221228
fSelectors := TCSSSelectorMap.Create(
222229
[doOwnsValues], TTextEqualityComparer.Create
223230
);
231+
fSelectorNames := TList<string>.Create;
224232
end;
225233

226234
destructor TCSSBuilder.Destroy;
227235
{Destructor. Tears down object.
228236
}
229237
begin
230-
fSelectors.Free; // frees selector objects in fSelectors.Values[]
238+
fSelectorNames.Free;
239+
fSelectors.Free; // frees owened selector objects in dictionary
231240
inherited;
232241
end;
233242

0 commit comments

Comments
 (0)