From 4d18029ae9a2a1ce4184ec82e1a4ec68c9707c86 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 12 Apr 2011 18:05:13 -0700 Subject: [PATCH] xmode.highlight: a syntax highlighting tool. --- basis/xmode/highlight/authors.txt | 1 + basis/xmode/highlight/highlight-docs.factor | 27 +++++++++ basis/xmode/highlight/highlight.factor | 61 +++++++++++++++++++++ basis/xmode/highlight/summary.txt | 1 + 4 files changed, 90 insertions(+) create mode 100644 basis/xmode/highlight/authors.txt create mode 100644 basis/xmode/highlight/highlight-docs.factor create mode 100644 basis/xmode/highlight/highlight.factor create mode 100644 basis/xmode/highlight/summary.txt diff --git a/basis/xmode/highlight/authors.txt b/basis/xmode/highlight/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/basis/xmode/highlight/authors.txt @@ -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 index 0000000000..06d6da45b0 --- /dev/null +++ b/basis/xmode/highlight/highlight-docs.factor @@ -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 index 0000000000..208abdefcb --- /dev/null +++ b/basis/xmode/highlight/highlight.factor @@ -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 + + + +: 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 index 0000000000..8cb354daed --- /dev/null +++ b/basis/xmode/highlight/summary.txt @@ -0,0 +1 @@ +Syntax highlighting tool