From 060c03ecf127b3ab5dc0d149cc77738237b27d9b Mon Sep 17 00:00:00 2001 From: christian_u Date: Fri, 31 Oct 2008 15:40:07 +0000 Subject: [PATCH] Initial import of wikihelp Tools git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@600 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- applications/wikihelp/src/wiki2html.lpi | 280 +++++++++++++++++++++ applications/wikihelp/src/wiki2html.lpr | 109 ++++++++ applications/wikihelp/src/wikidownload.lpi | 277 ++++++++++++++++++++ applications/wikihelp/src/wikidownload.lpr | 226 +++++++++++++++++ 4 files changed, 892 insertions(+) create mode 100644 applications/wikihelp/src/wiki2html.lpi create mode 100644 applications/wikihelp/src/wiki2html.lpr create mode 100644 applications/wikihelp/src/wikidownload.lpi create mode 100644 applications/wikihelp/src/wikidownload.lpr diff --git a/applications/wikihelp/src/wiki2html.lpi b/applications/wikihelp/src/wiki2html.lpi new file mode 100644 index 000000000..b7275bcdb --- /dev/null +++ b/applications/wikihelp/src/wiki2html.lpi @@ -0,0 +1,280 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/applications/wikihelp/src/wiki2html.lpr b/applications/wikihelp/src/wiki2html.lpr new file mode 100644 index 000000000..07e14ca13 --- /dev/null +++ b/applications/wikihelp/src/wiki2html.lpr @@ -0,0 +1,109 @@ +program wiki2html; + +{$mode objfpc}{$H+} + +uses + {$IFDEF UNIX}{$IFDEF UseCThreads} + cthreads, + {$ENDIF}{$ENDIF} + Classes, SysUtils, CustApp + { you can add units after this }, regex, regexpr, wikitohtml,Fileutil; + +type + + { TWiki2HTML } + + TWiki2HTML = class(TCustomApplication) + protected + procedure DoRun; override; + public + constructor Create(TheOwner: TComponent); override; + destructor Destroy; override; + procedure WriteHelp; virtual; + end; + +{ TWiki2HTML } + +procedure TWiki2HTML.DoRun; +var + ErrorMsg: String; + fs: TFileStream; + ss: TStringStream; + r: TRegexEngine; + index: longint; + len: longint; + tmp: ansistring; + Info: TSearchRec; +begin + // parse parameters + if HasOption('h','help') then begin + WriteHelp; + Halt; + end; + { add your program here } + if DirectoryExists(CleanAndExpandDirectory(GetParams(1))) then + begin + if FindFirst (CleanAndExpandDirectory(GetParams(1))+DirectorySeparator+'*.txt',faAnyFile,Info)=0 then + repeat + with Info do + begin + if not ((Attr and faDirectory) = faDirectory) then + begin + fs := TFilestream.Create(CleanAndExpandDirectory(GetParams(1))+Info.Name,fmOpenRead); + ss := TStringStream.Create(''); + ss.CopyFrom(fs,fs.Size); + fs.free; + tmp := wikitext2html(GetParams(1),ss.DataString); + ss.free; + ss := TStringStream.Create(tmp); + fs := TFileStream.Create(ChangeFileExt(CleanAndExpandDirectory(GetParams(1))+lowercase(Info.Name),'.html'),fmCreate); + fs.CopyFrom(ss,ss.Size); + fs.Free; + end; + end; + until FindNext(info)<>0; + FindClose(Info); + end + else + begin + fs := TFilestream.Create(GetParams(1),fmOpenRead); + ss := TStringStream.Create(''); + ss.CopyFrom(fs,fs.Size); + fs.free; + tmp := wikitext2html(GetParams(1),ss.DataString); + ss.free; + ss := TStringStream.Create(tmp); + fs := TFileStream.Create(ChangeFileExt(GetParams(1),'.html'),fmCreate); + fs.CopyFrom(ss,ss.Size); + fs.Free; + end; + // stop program loop + Terminate; +end; + +constructor TWiki2HTML.Create(TheOwner: TComponent); +begin + inherited Create(TheOwner); + StopOnException:=True; +end; + +destructor TWiki2HTML.Destroy; +begin + inherited Destroy; +end; + +procedure TWiki2HTML.WriteHelp; +begin + { add your help code here } + writeln('Usage: ',ExeName,' filename.txt'); +end; + +var + Application: TWiki2HTML; +begin + Application:=TWiki2HTML.Create(nil); + Application.Title:='Wiki2HTML'; + Application.Run; + Application.Free; +end. + diff --git a/applications/wikihelp/src/wikidownload.lpi b/applications/wikihelp/src/wikidownload.lpi new file mode 100644 index 000000000..7e7abdf86 --- /dev/null +++ b/applications/wikihelp/src/wikidownload.lpi @@ -0,0 +1,277 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/applications/wikihelp/src/wikidownload.lpr b/applications/wikihelp/src/wikidownload.lpr new file mode 100644 index 000000000..a30c26f8e --- /dev/null +++ b/applications/wikihelp/src/wikidownload.lpr @@ -0,0 +1,226 @@ +program wikidownload; + +{$mode objfpc}{$H+} + +uses + {$IFDEF UNIX}{$IFDEF UseCThreads} + cthreads, + {$ENDIF}{$ENDIF} + Classes, SysUtils, CustApp + { you can add units after this }, synapse, httpsend,xmlread,dom; + +type + + { TWikiDownload } + + TWikiDownload = class(TCustomApplication) + protected + procedure DoRun; override; + public + pages : TStringList; + constructor Create(TheOwner: TComponent); override; + destructor Destroy; override; + procedure WriteHelp; virtual; + procedure ExportPage(pagename : string); + end; + +{ TWikiDownload } + +procedure TWikiDownload.DoRun; +var + ErrorMsg: String; + ss: TStringStream; + http: THTTPSend; + s: String; + tmp: String; + i: Integer; +begin + // parse parameters + if HasOption('h','help') then begin + WriteHelp; + Halt; + end; + + { add your program here } + + ss := TStringStream.Create(''); + http := THttpSend.Create; + http.UserAgent := 'Mozilla/4.0 (compatible; WikiHelp)'; + http.HTTPMethod('GET',GetOptionValue('a','allpages')); + http.Document.SaveToStream(ss); + http.Free; + s := ss.DataString; + s := copy(s,pos('
',s),length(s)); + s := copy(s,0,pos('
',s)); + if s = '' then + begin + writeln('Special Page not found !'); + exit; + end; + ss.Free; + pages := TStringList.Create; + while pos(' 0 then + tmp := copy(tmp,pos('.php/',tmp)+5,length(tmp)); + if copy(tmp,0,length(GetOptionValue('o','pageoffset'))) = GetOptionValue('o','pageoffset') then + pages.Add(tmp); + s := copy(s,pos('"',s)+1,length(s)); + end; + if HasOption('r','recurse') then + begin + end + else + for i := 0 to pages.Count-1 do + ExportPage(pages[i]); + pages.free; + // stop program loop + Terminate; +end; + +constructor TWikiDownload.Create(TheOwner: TComponent); +begin + inherited Create(TheOwner); + StopOnException:=True; +end; + +destructor TWikiDownload.Destroy; +begin + inherited Destroy; +end; + +procedure TWikiDownload.WriteHelp; +begin + { add your help code here } + writeln('Usage: ',ExeName,' -h'); +end; + +function ValidateFileName(old : string) : string; +begin + Result := StringReplace(old,'\','',[rfReplaceAll]); + Result := StringReplace(Result,'/','',[rfReplaceAll]); + Result := StringReplace(Result,'@','',[rfReplaceAll]); + Result := StringReplace(Result,';','',[rfReplaceAll]); + Result := StringReplace(Result,'#','_',[rfReplaceAll]); + Result := StringReplace(Result,'>','_',[rfReplaceAll]); + Result := StringReplace(Result,'<','_',[rfReplaceAll]); + Result := StringReplace(Result,'|','_',[rfReplaceAll]); + Result := StringReplace(Result,'"','_',[rfReplaceAll]); + Result := StringReplace(Result,':','_',[rfReplaceAll]); + Result := StringReplace(Result,'*','_',[rfReplaceAll]); + Result := StringReplace(Result,'?','_',[rfReplaceAll]); +end; + +procedure TWikiDownload.ExportPage(pagename: string); +var + http: THTTPSend; + xml: TXMLDocument; + iNode: TDOMNode; + f : TextFile; + tmp: WideString; + istr: WideString; + + procedure ReplaceImages(ImageTagName : string); + begin + while pos('[['+ImageTagName+':',istr) > 0 do + begin + istr := copy(istr,pos('[['+ImageTagName+':',istr)+length(ImageTagname)+3,length(istr)); + if (pos('|',istr) > 0) and (pos('|',istr) < pos(']]',istr)) then + begin + http := THttpSend.Create; + http.HTTPMethod('GET',GetOptionValue('i','imagedir')+copy(istr,0,pos('|',istr)-1)); + http.Document.SaveToFile(GetOptionValue('o','output')+DirectorySeparator+ValidateFileName(lowercase(copy(istr,0,pos('|',istr)-1)))); + http.Free; + istr := copy(istr,pos(']]',istr)+2,length(istr)); + end + else + begin +{ aWikiStart := eWikiPage.Text; + if pos('index.php',aWikiStart) > 0 then + aWikiStart := copy(aWikiStart,0,pos('index.php',aWikiStart)-1);} + http := THttpSend.Create; + http.HTTPMethod('GET',GetOptionValue('i','imagedir')+copy(istr,0,pos(']]',istr)-1)); + http.Document.SaveToFile(StringReplace(trim(GetOptionValue('o','output')),#13,'',[rfReplaceAll])+DirectorySeparator+ValidateFileName(lowercase(copy(istr,0,pos(']]',istr)-1)))); + http.Free; + istr := copy(istr,pos(']]',istr)+2,length(istr)); + end; + end; + end; + + procedure AddLinksFromPage; + var + linkcontent: String; + begin + while pos('[[',istr) > 0 do + begin + istr := copy(istr,pos('[[',istr)+2,length(istr)); + if (pos('|',istr) > 0) and (pos('|',istr) < pos(']]',istr)) then + begin + linkcontent := copy(istr,0,pos('|',istr)-1); + if pages.IndexOf(linkcontent) = -1 then + pages.Add(linkcontent); + end + else + begin + linkcontent := copy(istr,0,pos(']]',istr)-1); + if pages.IndexOf(linkcontent) = -1 then + pages.Add(linkcontent); + end; + end; + end; +begin + http := THttpSend.Create; + http.UserAgent := 'Mozilla/4.0 (compatible; WikiHelp)'; + http.HTTPMethod('POST',GetOptionValue('e','exportpage')+'?action=submit&pages='+pagename+'&curonly=true'); + if http.ResultCode = 200 then + begin + xml := TXMLDocument.Create; + try + ReadXMLFile(xml,http.Document); + http.Free; + iNode := xml.DocumentElement.FindNode('page'); + if Assigned(iNode) then + iNode := iNode.FindNode('revision'); + if Assigned(iNode) then + iNode := iNode.FindNode('text'); + if Assigned(iNode) then + begin + AssignFile(f,StringReplace(trim(GetOptionValue('o','output')),#13,'',[rfReplaceAll])+DirectorySeparator+ValidateFilename(pagename)+'.txt'); + rewrite(f); + if Assigned(iNode.FirstChild) then + tmp := iNode.FirstChild.NodeValue + else + tmp := iNode.NodeValue; + writeln(f,tmp); + Closefile(f); + istr := tmp; + ReplaceImages('Bild'); + istr := tmp; + ReplaceImages('Image'); + istr := tmp; + if hasoption('r','recursive') then + AddLinksFromPage; + end + else + writeln('Page Node for: '+pagename+' not found !'); + xml.Free; + except + on e : Exception do + writeln('Error Processing :'+pagename+':'+e.Message+' ('+StringReplace(trim(GetOptionValue('o','output')),#13,'',[rfReplaceAll])+DirectorySeparator+ValidateFilename(pagename)+'.txt)'); + end; + end + else + writeln('Page: '+pagename+' not found !'); +end; + +var + Application: TWikiDownload; +begin + Application:=TWikiDownload.Create(nil); + Application.Title:='Wiki Download'; + Application.Run; + Application.Free; +end. +