]> gitweb.factorcode.org Git - factor.git/commitdiff
xmode.highlight: a syntax highlighting tool.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 13 Apr 2011 01:05:13 +0000 (18:05 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 13 Apr 2011 01:05:13 +0000 (18:05 -0700)
basis/xmode/highlight/authors.txt [new file with mode: 0644]
basis/xmode/highlight/highlight-docs.factor [new file with mode: 0644]
basis/xmode/highlight/highlight.factor [new file with mode: 0644]
basis/xmode/highlight/summary.txt [new file with mode: 0644]

diff --git a/basis/xmode/highlight/authors.txt b/basis/xmode/highlight/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/basis/xmode/highlight/highlight-docs.factor b/basis/xmode/highlight/highlight-docs.factor
new file mode 100644 (file)
index 0000000..06d6da4
--- /dev/null
@@ -0,0 +1,27 @@
+! Copyright (C) 2011 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: help.markup help.syntax sequences strings xmode.catalog
+xmode.highlight xmode.tokens ;
+
+IN: xmode.highlight
+
+HELP: highlight-tokens
+{ $values { "tokens" sequence } }
+{ $description
+    "Highlight a sequence of " { $link token } " objects."
+} ;
+
+HELP: highlight-lines
+{ $values { "lines" sequence } { "mode" string } }
+{ $description
+    "Highlight lines of code, according to the specified " { $link mode }
+    "."
+} ;
+
+HELP: highlight.
+{ $values { "path" string } }
+{ $description
+    "Highlight and print code from the specified file (represented by "
+    { $snippet "path" } ").  The mode is determined using the file extension."
+} ;
diff --git a/basis/xmode/highlight/highlight.factor b/basis/xmode/highlight/highlight.factor
new file mode 100644 (file)
index 0000000..208abde
--- /dev/null
@@ -0,0 +1,61 @@
+! Copyright (C) 2011 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: accessors assocs colors.hex io io.encodings.utf8 io.files
+io.styles kernel locals math math.parser namespaces sequences
+xmode.catalog xmode.marker ;
+
+IN: xmode.highlight
+
+<PRIVATE
+
+CONSTANT: STYLES H{
+    { "NULL"     H{ { foreground HEXCOLOR: 000000 } } }
+    { "COMMENT1" H{ { foreground HEXCOLOR: cc0000 } } }
+    { "COMMENT2" H{ { foreground HEXCOLOR: ff8400 } } }
+    { "COMMENT3" H{ { foreground HEXCOLOR: 6600cc } } }
+    { "COMMENT4" H{ { foreground HEXCOLOR: cc6600 } } }
+    { "DIGIT"    H{ { foreground HEXCOLOR: ff0000 } } }
+    { "FUNCTION" H{ { foreground HEXCOLOR: 9966ff } } }
+    { "INVALID"  H{ { background HEXCOLOR: ffffcc }
+                    { foreground HEXCOLOR: ff0066 } } }
+    { "KEYWORD1" H{ { foreground HEXCOLOR: 006699 }
+                    { font-style bold } } }
+    { "KEYWORD2" H{ { foreground HEXCOLOR: 009966 }
+                    { font-style bold } } }
+    { "KEYWORD3" H{ { foreground HEXCOLOR: 0099ff }
+                    { font-style bold } } }
+    { "KEYWORD4" H{ { foreground HEXCOLOR: 66ccff }
+                    { font-style bold } } }
+    { "LABEL"    H{ { foreground HEXCOLOR: 02b902 } } }
+    { "LITERAL1" H{ { foreground HEXCOLOR: ff00cc } } }
+    { "LITERAL2" H{ { foreground HEXCOLOR: cc00cc } } }
+    { "LITERAL3" H{ { foreground HEXCOLOR: 9900cc } } }
+    { "LITERAL4" H{ { foreground HEXCOLOR: 6600cc } } }
+    { "MARKUP"   H{ { foreground HEXCOLOR: 0000ff } } }
+    { "OPERATOR" H{ { foreground HEXCOLOR: 000000 }
+                    { font-style bold } } }
+}
+
+CONSTANT: BASE H{
+    { font-name "monospace" }
+}
+
+PRIVATE>
+
+: highlight-tokens ( tokens -- )
+    [
+        [ str>> ] [ id>> ] bi
+        [ name>> STYLES at ] [ f ] if* BASE assoc-union
+        format
+    ] each nl ;
+
+: highlight-lines ( lines mode -- )
+    [ f ] 2dip load-mode [
+        tokenize-line highlight-tokens
+    ] curry each drop ;
+
+:: highlight. ( path -- )
+    path utf8 file-lines [
+        path over first find-mode highlight-lines
+    ] unless-empty ;
diff --git a/basis/xmode/highlight/summary.txt b/basis/xmode/highlight/summary.txt
new file mode 100644 (file)
index 0000000..8cb354d
--- /dev/null
@@ -0,0 +1 @@
+Syntax highlighting tool