You've already forked lazarus-ccr
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:
@ -516,7 +516,7 @@ object MainFrm: TMainFrm
|
||||
end
|
||||
object OpenDialog: TOpenDialog
|
||||
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]
|
||||
left = 184
|
||||
top = 200
|
||||
|
@ -0,0 +1,3 @@
|
||||
|| || title1 || title2 || title3
|
||||
| [link_to_something|http://google.com]| {color:red}FAILED| {color:red}FAILED| {color:green}PASS
|
||||
|
@ -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,11 +180,11 @@ 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 ||
|
||||
wtsLineStart: // Line-start or otherwise reading a pipe separator, expecting a | or ||
|
||||
begin
|
||||
if lCurChar = Str_Pipe then
|
||||
begin
|
||||
lState := 1;
|
||||
lState := wtsCellText;
|
||||
lIsTitle := False;
|
||||
if lLookAheadChar = Str_Pipe then
|
||||
begin
|
||||
@ -218,21 +207,22 @@ begin
|
||||
raise Exception.Create('[TWikiTableTokenizer.TokenizeString] Wrong char!');
|
||||
end;
|
||||
end;
|
||||
1: // Reading cell text
|
||||
|
||||
wtsCellText: // Reading cell text
|
||||
begin
|
||||
if lCurChar = Str_Pipe then
|
||||
begin
|
||||
lState := 0;
|
||||
lState := wtsLineStart;
|
||||
DoAddToken();
|
||||
end
|
||||
else if lCurChar = Str_LinkStart then
|
||||
begin
|
||||
lState := 2;
|
||||
lState := wtsLinkText;
|
||||
Inc(i);
|
||||
end
|
||||
else if lCurChar = Str_FormatStart then
|
||||
begin
|
||||
lState := 4;
|
||||
lState := wtsColor;
|
||||
Inc(i);
|
||||
end
|
||||
else
|
||||
@ -241,11 +231,12 @@ begin
|
||||
Inc(i);
|
||||
end;
|
||||
end;
|
||||
2: // Link text reading
|
||||
|
||||
wtsLinkText: // Link text reading
|
||||
begin
|
||||
if lCurChar = Str_Pipe then
|
||||
begin
|
||||
lState := 3;
|
||||
lState := wtsLinkTarget;
|
||||
Inc(i);
|
||||
end
|
||||
else
|
||||
@ -254,11 +245,12 @@ begin
|
||||
Inc(i);
|
||||
end;
|
||||
end;
|
||||
3: // Link target reading
|
||||
|
||||
wtsLinkTarget: // Link target reading
|
||||
begin
|
||||
if lCurChar = Str_LinkEnd then
|
||||
begin
|
||||
lState := 1;
|
||||
lState := wtsCellText;
|
||||
Inc(i);
|
||||
end
|
||||
else
|
||||
@ -266,21 +258,21 @@ begin
|
||||
Inc(i);
|
||||
end;
|
||||
end;
|
||||
4: // Color start reading
|
||||
|
||||
wtsColor: // Color start reading
|
||||
begin
|
||||
if lCurChar = Str_FormatEnd then
|
||||
begin
|
||||
lState := 1;
|
||||
lState := wtsCellText;
|
||||
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;
|
||||
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
|
||||
@ -288,24 +280,19 @@ begin
|
||||
Inc(i);
|
||||
end;
|
||||
end;
|
||||
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);
|
||||
|
Reference in New Issue
Block a user