extractlang.lpr 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (c) 2019 by Michael Van Canneyt
  4. Program to extract data-translate tags from a HTML file.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program extractlang;
  12. {$mode objfpc}{$H+}
  13. uses
  14. {$IFDEF UNIX}
  15. cwstring,
  16. {$ENDIF}
  17. Classes, SysUtils, CustApp, jsonparser, langextractor;
  18. type
  19. { TExtractLangApplication }
  20. TExtractLangApplication = class(TCustomApplication)
  21. private
  22. procedure Logger({%H-}Sender: TObject; const Msg: String);
  23. protected
  24. FExtractor : THTMLLangExtractor;
  25. procedure DoRun; override;
  26. public
  27. constructor Create(TheOwner: TComponent); override;
  28. destructor Destroy; override;
  29. procedure Usage(Const Msg : String); virtual;
  30. end;
  31. { TExtractLangApplication }
  32. procedure TExtractLangApplication.Logger(Sender: TObject; const Msg: String);
  33. begin
  34. Writeln(Msg);
  35. end;
  36. procedure TExtractLangApplication.DoRun;
  37. var
  38. ErrorMsg: String;
  39. begin
  40. Terminate;
  41. ErrorMsg:=CheckOptions('cd:f:hl:mn:o:ts:r', ['clear','file-mode','help','html-dir','languages','minify','name','output','recurse','single-scope','trash-values']);
  42. if (ErrorMsg<>'') or HasOption('h','help') then
  43. begin
  44. Usage(ErrorMsg);
  45. exit;
  46. end;
  47. With FExtractor do
  48. begin
  49. OnLog:=@Logger;
  50. HTMLDir:=GetOptionValue('d','html-dir');
  51. OutputFileName:=GetOptionValue('o','output');
  52. Languages:=GetOptionValue('l','languages');
  53. Minified:=HasOption('m','minify');
  54. TrashNewValues:=HasOption('t','trash-values');
  55. SingleScope:=GetOptionValue('s','single-scope');
  56. CleanOutput:=HasOption('c','clear');
  57. Recurse:=HasOption('r','recurse');
  58. TagName:=GetOptionValue('n','name');
  59. if (HTMLDir='') or (OutputFileName='') then
  60. Usage('Need input dir and output filename');
  61. if HasOption('f','file-mode') then
  62. Case LowerCase(GetOptionValue('f','file-mode')) of
  63. 'single':
  64. OutputFileMode:=fmSingle;
  65. 'multiple',
  66. 'multi':
  67. OutputFileMode:=fmMultiple;
  68. else
  69. OutputFileMode:=fmSingle;
  70. end;
  71. TrashNewValues:=HasOption('t','trash-values');
  72. Execute;
  73. end;
  74. end;
  75. constructor TExtractLangApplication.Create(TheOwner: TComponent);
  76. begin
  77. inherited Create(TheOwner);
  78. StopOnException:=True;
  79. FExtractor:=THTMLLangExtractor.Create(Self);
  80. end;
  81. destructor TExtractLangApplication.Destroy;
  82. begin
  83. FreeAndNil(FExtractor);
  84. inherited Destroy;
  85. end;
  86. procedure TExtractLangApplication.Usage(const Msg: String);
  87. begin
  88. if Msg<>'' then
  89. Writeln('Error : ',Msg);
  90. Writeln('Usage: ', ExeName, ' [options]');
  91. Writeln('Where options is one or more of:');
  92. Writeln('-h --help This help text');
  93. Writeln('-c --clear Clear output JSON file (Default is to update existing output file).');
  94. Writeln('-d --html-dir=DIR Directory with HTML files to scan (recursively)');
  95. Writeln('-f --file-mode=MODE Set file mode: one of single or multiple');
  96. Writeln('-o --output=FILE File to write JSON translations (may get suffix depending on file mode)');
  97. Writeln('-l --languages=LIST Comma-separated list of languages to create');
  98. Writeln('-m --minify Minify output');
  99. Writeln('-n --name=NAME Set name of data-tag to NAME (data-NAME)');
  100. Writeln('-r --recurse Recurse into subdirectories of the HTML directory');
  101. Writeln('-s --single-scope=SCOPE Put all translation names in a single scope');
  102. Writeln('-t --trash-values Trash values for other languages');
  103. ExitCode:=Ord(Msg<>'');
  104. Halt;
  105. end;
  106. var
  107. Application: TExtractLangApplication;
  108. begin
  109. Application:=TExtractLangApplication.Create(nil);
  110. Application.Title:='Extract data-translate tag application';
  111. Application.Run;
  112. Application.Free;
  113. end.