fpspreadsheet: Add wikitable_pipes demo file. Extend wikitable simple_pipes reader to show all background colors. Some cosmetics in wikitable.pas

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3641 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2014-10-08 14:21:13 +00:00
parent 390b0539f1
commit c98ddb84d2
3 changed files with 125 additions and 135 deletions

View File

@ -35,6 +35,8 @@ uses
type
TWikiTokenState = (wtsLineStart, wtsCellText, wtsLinkText, wtsLinkTarget, wtsColor);
TWikiTableToken = class
public
BackgroundColor: TsColor;
@ -48,29 +50,20 @@ type
{ TWikiTableTokenizer }
TWikiTableTokenizer = class
private
FWorkbook: TsWorkbook;
public
Tokens: TWikiTableTokenList;
constructor Create; virtual;
constructor Create(AWorkbook: TsWorkbook); virtual;
destructor Destroy; override;
procedure Clear;
function AddToken(AValue: string): TWikiTableToken;
procedure TokenizeString_Pipes(AStr: string);
end;
{ TsWikiTableNumFormatList }
TsWikiTableNumFormatList = class(TsCustomNumFormatList)
protected
// procedure AddBuiltinFormats; override;
public
// function FormatStringForWriting(AIndex: Integer): String; override;
end;
{ TsWikiTableReader }
TsWikiTableReader = class(TsCustomSpreadReader)
protected
procedure CreateNumFormatList; override;
public
SubFormat: TsSpreadsheetFormat;
{ General reading methods }
@ -88,9 +81,6 @@ type
{ TsWikiTableWriter }
TsWikiTableWriter = class(TsCustomSpreadWriter)
protected
// Helpers
procedure CreateNumFormatList; override;
public
SubFormat: TsSpreadsheetFormat;
{ General writing methods }
@ -111,14 +101,12 @@ uses
fpsStrings;
{ TsWikiTableNumFormatList }
//...
{ TWikiTableTokenizer }
constructor TWikiTableTokenizer.Create;
constructor TWikiTableTokenizer.Create(AWorkbook: TsWorkbook);
begin
inherited Create;
FWorkbook := AWorkbook;
Tokens := TWikiTableTokenList.Create;
end;
@ -163,7 +151,8 @@ var
i: Integer;
lTmpStr: string = '';
lFormatStr: string = '';
lState: Integer;
lColorStr: String = '';
lState: TWikiTokenState;
lLookAheadChar, lCurChar: Char;
lIsTitle: Boolean = False;
lCurBackgroundColor: TsColor;
@ -182,7 +171,7 @@ var
begin
Clear;
lState := 0;
lState := wtsLineStart;
i := 1;
while i <= Length(AStr) do
@ -191,121 +180,119 @@ begin
if i < Length(AStr) then lLookAheadChar := AStr[i+1];
case lState of
0: // Line-start or otherwise reading a pipe separator, expecting a | or ||
begin
if lCurChar = Str_Pipe then
begin
lState := 1;
lIsTitle := False;
if lLookAheadChar = Str_Pipe then
wtsLineStart: // Line-start or otherwise reading a pipe separator, expecting a | or ||
begin
Inc(i);
lIsTitle := True;
end;
Inc(i);
if lCurChar = Str_Pipe then
begin
lState := wtsCellText;
lIsTitle := False;
if lLookAheadChar = Str_Pipe then
begin
Inc(i);
lIsTitle := True;
end;
Inc(i);
lUseBackgroundColor := False;
lTmpStr := '';
end
else if lCurChar in Str_EmptySpaces then
begin
// Do nothing
Inc(i);
end
else
begin
// Error!!!
raise Exception.Create('[TWikiTableTokenizer.TokenizeString] Wrong char!');
end;
end;
1: // Reading cell text
begin
if lCurChar = Str_Pipe then
begin
lState := 0;
DoAddToken();
end
else if lCurChar = Str_LinkStart then
begin
lState := 2;
Inc(i);
end
else if lCurChar = Str_FormatStart then
begin
lState := 4;
Inc(i);
end
else
begin
lTmpStr := lTmpStr + lCurChar;
Inc(i);
end;
end;
2: // Link text reading
begin
if lCurChar = Str_Pipe then
begin
lState := 3;
Inc(i);
end
else
begin
lTmpStr := lTmpStr + lCurChar;
Inc(i);
end;
end;
3: // Link target reading
begin
if lCurChar = Str_LinkEnd then
begin
lState := 1;
Inc(i);
end
else
begin
Inc(i);
end;
end;
4: // Color start reading
begin
if lCurChar = Str_FormatEnd then
begin
lState := 1;
Inc(i);
lFormatStr := LowerCase(Trim(lFormatStr));
if lFormatStr = 'color:red' then lCurBackgroundColor := scRED
else if lFormatStr = 'color:green' then lCurBackgroundColor := scGREEN
else if lFormatStr = 'color:yellow' then lCurBackgroundColor := scYELLOW
//
else if lFormatStr = 'color:orange' then lCurBackgroundColor := scOrange
else lCurBackgroundColor := scWHITE;
lUseBackgroundColor := True;
lFormatStr := '';
end
else
begin
lFormatStr := lFormatStr + lCurChar;
Inc(i);
end;
end;
end;
end;
lUseBackgroundColor := False;
lTmpStr := '';
end
else if lCurChar in Str_EmptySpaces then
begin
// Do nothing
Inc(i);
end
else
begin
// Error!!!
raise Exception.Create('[TWikiTableTokenizer.TokenizeString] Wrong char!');
end;
end;
wtsCellText: // Reading cell text
begin
if lCurChar = Str_Pipe then
begin
lState := wtsLineStart;
DoAddToken();
end
else if lCurChar = Str_LinkStart then
begin
lState := wtsLinkText;
Inc(i);
end
else if lCurChar = Str_FormatStart then
begin
lState := wtsColor;
Inc(i);
end
else
begin
lTmpStr := lTmpStr + lCurChar;
Inc(i);
end;
end;
wtsLinkText: // Link text reading
begin
if lCurChar = Str_Pipe then
begin
lState := wtsLinkTarget;
Inc(i);
end
else
begin
lTmpStr := lTmpStr + lCurChar;
Inc(i);
end;
end;
wtsLinkTarget: // Link target reading
begin
if lCurChar = Str_LinkEnd then
begin
lState := wtsCellText;
Inc(i);
end
else
begin
Inc(i);
end;
end;
wtsColor: // Color start reading
begin
if lCurChar = Str_FormatEnd then
begin
lState := wtsCellText;
Inc(i);
lFormatStr := LowerCase(Trim(lFormatStr));
if copy(lFormatstr, 1, 6) = 'color:' then
begin
lColorstr := Copy(lFormatstr, 7, Length(lFormatStr));
lCurBackgroundColor := FWorkbook.AddColorToPalette(HTMLColorStrToColor(lColorStr));
lUseBackgroundColor := True;
lFormatStr := '';
end;
end
else
begin
lFormatStr := lFormatStr + lCurChar;
Inc(i);
end;
end;
end; // case
end; // while
// rest after the last || is also a token
if lTmpStr <> '' then DoAddToken();
// If there is a token still to be added, add it now
if (lState = 0) and (lTmpStr <> '') then AddToken(lTmpStr);
if (lState = wtsLineStart) and (lTmpStr <> '') then AddToken(lTmpStr);
end;
{ TsWikiTableReader }
procedure TsWikiTableReader.CreateNumFormatList;
begin
FreeAndNil(FNumFormatList);
FNumFormatList := TsWikiTableNumFormatList.Create(Workbook);
end;
procedure TsWikiTableReader.ReadFromStrings(AStrings: TStrings;
AData: TsWorkbook);
begin
@ -323,7 +310,7 @@ var
lCurToken: TWikiTableToken;
begin
FWorksheet := AData.AddWorksheet('Table');
lLineSplitter := TWikiTableTokenizer.Create;
lLineSplitter := TWikiTableTokenizer.Create(AData);
try
for i := 0 to AStrings.Count-1 do
begin
@ -333,8 +320,10 @@ begin
begin
lCurToken := lLineSplitter.Tokens[j];
FWorksheet.WriteUTF8Text(i, j, lCurToken.Value);
if lCurToken.Bold then FWorksheet.WriteUsedFormatting(i, j, [uffBold]);
if lCurToken.UseBackgroundColor then FWorksheet.WriteBackgroundColor(i, j, lCurToken.BackgroundColor);
if lCurToken.Bold then
FWorksheet.WriteFontStyle(i, j, [fssBold]);
if lCurToken.UseBackgroundColor then
FWorksheet.WriteBackgroundColor(i, j, lCurToken.BackgroundColor);
end;
end;
finally
@ -342,6 +331,7 @@ begin
end;
end;
{ TsWikiTable_PipesReader }
constructor TsWikiTable_PipesReader.Create(AWorkbook: TsWorkbook);
@ -350,13 +340,8 @@ begin
SubFormat := sfWikiTable_Pipes;
end;
{ TsWikiTableWriter }
procedure TsWikiTableWriter.CreateNumFormatList;
begin
FreeAndNil(FNumFormatList);
FNumFormatList := TsWikiTableNumFormatList.Create(Workbook);
end;
{ TsWikiTableWriter }
procedure TsWikiTableWriter.WriteToStrings(AStrings: TStrings);
begin
@ -603,6 +588,7 @@ begin
AStrings.Add('|}');
end;
{ TsWikiTable_WikiMediaWriter }
constructor TsWikiTable_WikiMediaWriter.Create(AWorkbook: TsWorkbook);
@ -611,6 +597,7 @@ begin
SubFormat := sfWikiTable_WikiMedia;
end;
initialization
RegisterSpreadFormat(TsWikiTable_PipesReader, nil, sfWikiTable_Pipes);