diff --git a/components/fpspreadsheet/examples/spready/mainform.lfm b/components/fpspreadsheet/examples/spready/mainform.lfm index f05039107..51d03ba73 100644 --- a/components/fpspreadsheet/examples/spready/mainform.lfm +++ b/components/fpspreadsheet/examples/spready/mainform.lfm @@ -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 diff --git a/components/fpspreadsheet/examples/wikitabledemo/test.wikitable_pipes b/components/fpspreadsheet/examples/wikitabledemo/test.wikitable_pipes new file mode 100644 index 000000000..75739948b --- /dev/null +++ b/components/fpspreadsheet/examples/wikitabledemo/test.wikitable_pipes @@ -0,0 +1,3 @@ +|| || title1 || title2 || title3 +| [link_to_something|http://google.com]| {color:red}FAILED| {color:red}FAILED| {color:green}PASS + \ No newline at end of file diff --git a/components/fpspreadsheet/wikitable.pas b/components/fpspreadsheet/wikitable.pas index 3183905ce..5fc1ee653 100644 --- a/components/fpspreadsheet/wikitable.pas +++ b/components/fpspreadsheet/wikitable.pas @@ -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);