1 /// Author: Aziz Köksal 2 /// License: GPL3 3 /// $(Maturity average) 4 module cmd.Highlight; 5 6 import cmd.Command; 7 import dil.Highlighter; 8 import dil.Diagnostics; 9 import dil.Compilation; 10 import SettingsLoader; 11 import Settings; 12 import common; 13 14 import std.file; 15 16 /// The highlight command. 17 class HighlightCommand : Command 18 { 19 /// Options for the command. 20 enum Option 21 { 22 None = 0, 23 Tokens = 1, 24 Syntax = 1<<1, 25 HTML = 1<<2, 26 XML = 1<<3, 27 PrintLines = 1<<4 28 } 29 alias Options = Option; 30 31 Options options; /// Command options. 32 cstring filePathSrc; /// File path to the module to be highlighted. 33 cstring filePathDest; /// Where to write the highlighted file. 34 Diagnostics diag; /// Collects error messages. 35 CompilationContext cc; /// The context. 36 37 /// Adds o to the options. 38 void add(Option o) 39 { 40 options |= o; 41 } 42 43 /// Executes the command. 44 override void run() 45 { 46 add(HighlightCommand.Option.Tokens); 47 if (!(options & (Option.XML | Option.HTML))) 48 add(Option.HTML); // Default to HTML. 49 50 auto mapFilePath = options & Option.HTML ? GlobalSettings.htmlMapFile 51 : GlobalSettings.xmlMapFile; 52 auto map = TagMapLoader(cc, diag).load(mapFilePath); 53 auto tags = new TagMap(map); 54 55 if (diag.hasInfo) 56 return; 57 58 auto hl = new Highlighter(tags, cc); 59 60 bool printLines = (options & Option.PrintLines) != 0; 61 bool printHTML = (options & Option.HTML) != 0; 62 if (options & Option.Syntax) 63 hl.highlightSyntax(filePathSrc, printHTML, printLines); 64 else 65 hl.highlightTokens(filePathSrc, printLines); 66 67 auto text = hl.takeText(); 68 69 if (filePathDest.length) 70 filePathDest.write(text); 71 else 72 Stdout(text); 73 } 74 }