Skip to content

Commit 3e36b58

Browse files
committed
Commit WIP to use highlights instead of pygmentize
1 parent 8423f47 commit 3e36b58

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

highlights/highlight-folder.coffee

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Highlights = require 'highlights'
2+
fs = require 'fs'
3+
path = require 'path'
4+
highlighter = new Highlights()
5+
highlighter.requireGrammarsSync
6+
modulePath: require.resolve('./atom-language-perl6/package.json')
7+
8+
TestFolder = path.resolve('TestFolder')
9+
files = fs.readdirSync(TestFolder)
10+
11+
for file in files
12+
foo = ->
13+
fs.readFileSync path.resolve(TestFolder, file), 'utf8'
14+
html = highlighter.highlightSync
15+
fileContents: foo()
16+
scopeName: 'source.perl6fe'
17+
18+
console.log html

htmlify.p6

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ sub MAIN(
134134
Bool :$search-file = True,
135135
Bool :$no-highlight = False,
136136
Bool :$no-inline-python = False,
137+
Bool :$use-highlights = False,
137138
Int :$parallel = 1,
138139
) {
139140

@@ -163,7 +164,7 @@ sub MAIN(
163164
process-pod-dir 'Language', :$sparse, :$parallel;
164165
process-pod-dir 'Type', :sorted-by{ %h{.key} // -1 }, :$sparse, :$parallel;
165166

166-
highlight-code-blocks(:use-inline-python(!$no-inline-python)) unless $no-highlight;
167+
highlight-code-blocks(:use-inline-python(!$no-inline-python), :use-highlights($use-highlights)) unless $no-highlight;
167168

168169
say 'Composing doc registry ...';
169170
$*DR.compose;
@@ -946,41 +947,50 @@ sub write-qualified-method-call(:$name!, :$pod!, :$type!) {
946947
spurt "html/routine/{escape-filename $type}.{escape-filename $name}.html", p2h($p, 'routine');
947948
}
948949

949-
sub highlight-code-blocks(:$use-inline-python = True) {
950-
my $pyg-version = try qx/pygmentize -V/;
951-
if $pyg-version && $pyg-version ~~ /^'Pygments version ' (\d\S+)/ {
952-
if Version.new(~$0) ~~ v2.0+ {
953-
say "pygmentize $0 found; code blocks will be highlighted";
950+
sub highlight-code-blocks(:$use-inline-python = True, :$use-highlights = False) {
951+
say "highlight-code-blocks has been called";
952+
my $py;
953+
if $use-highlights {
954+
note "Using highlights";
955+
#return;
956+
}
957+
else {
958+
my $pyg-version = try qx/pygmentize -V/;
959+
if $pyg-version && $pyg-version ~~ /^'Pygments version ' (\d\S+)/ {
960+
if Version.new(~$0) ~~ v2.0+ {
961+
say "pygmentize $0 found; code blocks will be highlighted";
962+
}
963+
else {
964+
say "pygmentize $0 is too old; need at least 2.0";
965+
return;
966+
}
954967
}
955968
else {
956-
say "pygmentize $0 is too old; need at least 2.0";
969+
say "pygmentize not found; code blocks will not be highlighted";
957970
return;
958971
}
959-
}
960-
else {
961-
say "pygmentize not found; code blocks will not be highlighted";
962-
return;
963-
}
964-
965-
my $py = $use-inline-python && try {
966-
require Inline::Python;
967-
my $inline-py = ::('Inline::Python').new();
968-
$inline-py.run(q{
969-
import pygments.lexers
970-
import pygments.formatters
971-
p6lexer = pygments.lexers.get_lexer_by_name("perl6")
972-
htmlformatter = pygments.formatters.get_formatter_by_name("html")
973972

974-
def p6format(code):
975-
return pygments.highlight(code, p6lexer, htmlformatter)
976-
});
977-
$inline-py;
978-
}
979-
if $py {
980-
say "Using syntax highlighting via Inline::Python";
981-
}
982-
else {
983-
say "Error using Inline::Python, falling back to pygmentize: ($!)";
973+
$py = $use-inline-python && try {
974+
require Inline::Python;
975+
my $inline-py = ::('Inline::Python').new();
976+
$inline-py.run(Q:to/END/
977+
import pygments.lexers
978+
import pygments.formatters
979+
p6lexer = pygments.lexers.get_lexer_by_name("perl6")
980+
htmlformatter = pygments.formatters.get_formatter_by_name("html")
981+
982+
def p6format(code):
983+
return pygments.highlight(code, p6lexer, htmlformatter)
984+
END
985+
);
986+
$inline-py;
987+
}
988+
if $py {
989+
say "Using syntax highlighting via Inline::Python";
990+
}
991+
else {
992+
say "Error using Inline::Python, falling back to pygmentize: ($!)";
993+
}
984994
}
985995

986996
%*POD2HTML-CALLBACKS = code => sub (:$node, :&default) {
@@ -998,8 +1008,16 @@ def p6format(code):
9981008
my $tmp_fname = "$*TMPDIR/$basename";
9991009
spurt $tmp_fname, $node.contents.join;
10001010
LEAVE try unlink $tmp_fname;
1001-
my $command = "pygmentize -l perl6 -f html < $tmp_fname";
1002-
qqx{$command};
1011+
my $command;
1012+
if $use-highlights {
1013+
$command = "./highlights/node_modules/highlights/bin/highlights -i ./highlights/atom-language-perl6/package.json -s source.perl6fe -f ./highlights/atom-language-perl6/package.json < $tmp_fname"
1014+
}
1015+
else {
1016+
$command = "pygmentize -l perl6 -f html < $tmp_fname";
1017+
}
1018+
my $thing = qqx{$command};
1019+
say "OUTPUT OF HIGHLIGHTS: $thing";
1020+
$thing;
10031021
}
10041022
}
10051023
}

0 commit comments

Comments
 (0)