import frontend
import astutil
+import console
# Visitor used to produce a HTML tree based on a AST on a `Source`
class AbstractHighlightVisitor
# Low-level highlighting between 2 tokens
protected fun do_highlight(first_token: Token, last_token: nullable Token) is abstract
end
+
+# Text-based highlighter that use ANSI escape sequence for colors
+class AnsiHighlightVisitor
+ super AbstractHighlightVisitor
+
+ # The produced highlighting
+ var result = new Template
+
+ redef fun do_highlight(f, l)
+ do
+ var c
+ c = f
+ while c != null do
+ if c != f then result.add(c.blank_before)
+ result.add c.ansi_colored
+
+ if c == l then
+ c = null
+ else
+ c = c.next_token
+ end
+ end
+ end
+end
+
+redef class Token
+ # Return the ANSI colored text
+ fun ansi_colored: String do return text
+end
+
+redef class TComment
+ redef fun ansi_colored do return super.blue
+end
+
+redef class TokenKeyword
+ redef fun ansi_colored do return super.yellow
+end
+
+redef class TClassid
+ redef fun ansi_colored do return super.green
+end
+
+redef class TokenLiteral
+ redef fun ansi_colored do return super.red
+end
var opt_dir = new OptionString("Output html files in a specific directory (required if more than one module)", "-d", "--dir")
var opt_full = new OptionBool("Process also imported modules", "--full")
var opt_ast = new OptionBool("Generate specific HTML elements for each Node of the AST", "--ast")
-toolcontext.option_context.add_option(opt_fragment, opt_line_id_prefix, opt_first_line, opt_last_line, opt_dir, opt_full)
+var opt_txt = new OptionBool("Generate text with ANSI coloring escape sequences", "--txt")
+toolcontext.option_context.add_option(opt_fragment, opt_line_id_prefix, opt_first_line, opt_last_line, opt_dir, opt_full, opt_ast, opt_txt)
toolcontext.tooldescription = "Usage: nitlight [OPTION]... <file.nit>...\nGenerates HTML of highlited code from Nit source files."
toolcontext.process_options(args)
return
end
+if opt_txt.value then
+ for mm in mmodules do
+ var v = new AnsiHighlightVisitor
+ v.include_loose_tokens = true
+ v.include_whole_lines = true
+
+ if opt_first_line.value != 0 then v.first_line = opt_first_line.value
+ if opt_last_line.value != 0 then v.last_line = opt_last_line.value
+ var m = modelbuilder.mmodule2node(mm)
+ assert m != null
+
+ v.highlight_node(m)
+ var page = v.result
+
+ if dir != null then
+ page.write_to_file("{dir}/{mm.c_name}.txt")
+ else
+ page.write_to(stdout)
+ end
+ end
+ return
+end
+
for mm in mmodules do
if dir != null then toolcontext.info("write {dir}/{mm.c_name}.html", 1)
-f base_simple3.nit
base_simple3.nit
-f --line-id-prefix XYZ --first-line 38 --last-line 46 base_simple3.nit
+--txt base_simple3.nit
--- /dev/null
+\e[34m# This file is part of NIT ( http://www.nitlanguage.org ).
+\e[m\e[34m#
+\e[m\e[34m# Copyright 2006-2008 Jean Privat <jean@pryen.org>
+\e[m\e[34m#
+\e[m\e[34m# Licensed under the Apache License, Version 2.0 (the "License");
+\e[m\e[34m# you may not use this file except in compliance with the License.
+\e[m\e[34m# You may obtain a copy of the License at
+\e[m\e[34m#
+\e[m\e[34m# http://www.apache.org/licenses/LICENSE-2.0
+\e[m\e[34m#
+\e[m\e[34m# Unless required by applicable law or agreed to in writing, software
+\e[m\e[34m# distributed under the License is distributed on an "AS IS" BASIS,
+\e[m\e[34m# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+\e[m\e[34m# See the License for the specific language governing permissions and
+\e[m\e[34m# limitations under the License.
+\e[m
+\e[33mimport\e[m \e[33mend\e[m
+
+\e[33minterface\e[m \e[32mObject\e[m
+\e[33mend\e[m
+
+\e[33menum\e[m \e[32mBool\e[m
+\e[33mend\e[m
+
+\e[33menum\e[m \e[32mInt\e[m
+ \e[33mfun\e[m output \e[33mis\e[m intern
+\e[33mend\e[m
+
+\e[33mclass\e[m \e[32mA\e[m
+ \e[33minit\e[m \e[33mdo\e[m \e[31m5\e[m.output
+ \e[33mfun\e[m run \e[33mdo\e[m \e[31m6\e[m.output
+\e[33mend\e[m
+
+\e[33mclass\e[m \e[32mB\e[m
+ \e[33mvar\e[m val: \e[32mInt\e[m
+ \e[33minit\e[m(v: \e[32mInt\e[m)
+ \e[33mdo\e[m
+ \e[31m7\e[m.output
+ \e[33mself\e[m.val = v
+ \e[33mend\e[m
+ \e[33mfun\e[m run \e[33mdo\e[m val.output
+\e[33mend\e[m
+
+\e[33mclass\e[m \e[32mC\e[m
+ \e[33mvar\e[m val1: \e[32mInt\e[m
+ \e[33mvar\e[m val2: \e[32mInt\e[m = \e[31m10\e[m
+\e[33mend\e[m
+
+\e[33mfun\e[m foo \e[33mdo\e[m \e[31m2\e[m.output
+\e[33mfun\e[m bar(i: \e[32mInt\e[m) \e[33mdo\e[m i.output
+\e[33mfun\e[m baz: \e[32mInt\e[m \e[33mdo\e[m \e[33mreturn\e[m \e[31m4\e[m
+
+\e[31m1\e[m.output
+foo
+bar(\e[31m3\e[m)
+baz.output
+
+\e[33mvar\e[m a = \e[33mnew\e[m \e[32mA\e[m
+a.run
+
+\e[33mvar\e[m b = \e[33mnew\e[m \e[32mB\e[m(\e[31m8\e[m)
+b.run
+
+\e[33mvar\e[m c = \e[33mnew\e[m \e[32mC\e[m(\e[31m9\e[m)
+c.val1.output
+c.val2.output