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

@ -516,7 +516,7 @@ object MainFrm: TMainFrm
end end
object OpenDialog: TOpenDialog object OpenDialog: TOpenDialog
DefaultExt = '.xls' DefaultExt = '.xls'
Filter = 'Excel spreadsheet (*.xls)|*.xls|Excel XML spreadsheet (*.xlsx)|*.xlsx|LibreOffice/OpenOffice spreadsheet (*.ods)|*.ods|Wikitable (pipes) (.wikitable_pipes)|.wikitable_pipes|All files (*.*)|*.*' Filter = 'Excel spreadsheet (*.xls)|*.xls|Excel XML spreadsheet (*.xlsx)|*.xlsx|LibreOffice/OpenOffice spreadsheet (*.ods)|*.ods|Wikitable (pipes) (.wikitable_pipes)|*.wikitable_pipes|All files (*.*)|*.*'
Options = [ofExtensionDifferent, ofEnableSizing, ofViewDetail] Options = [ofExtensionDifferent, ofEnableSizing, ofViewDetail]
left = 184 left = 184
top = 200 top = 200

View File

@ -0,0 +1,3 @@
|| || title1 || title2 || title3
| [link_to_something|http://google.com]| {color:red}FAILED| {color:red}FAILED| {color:green}PASS

View File

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