class
ArduinoIDEParser
def
initialize(*ref_dirs)
@keywords_txts
= []
ref_dirs.
each
do
|dir|
@keywords_txts
+=
Dir
.glob(
File
.join(dir,
"**/keywords.txt"
)).to_a
end
@tokens
=
Hash
.
new
{|h, k| h[k] = []}
@keywords_txts
.
each
do
|f|
File
.readlines(f).map(&
:chomp
).
select{|l| l[
0
] !=
"#"
&& !l.strip.empty? && l.match(/^[a-zA-
Z_
]/)}.
map{|l| l.split(/\s+/)}.
each
do
|l|
@tokens
[l[
1
]] << l[
0
]
unless
@tokens
[l[
1
]].include?(l[
0
])
end
end
@ref_dirs
= ref_dirs
end
def
header
header_str = <<~
'EOS'
% Arduino language style
for
listings package
__GENERATED_FROM__
% usage: \lstinputlisting[language=Arduino]{somesketch.ino}
%
__KEYWORDS_TXTS__
\usepackage[dvipdfmx]{color}
\definecolor{lst-arduino-sngq}{rgb}{
0
.
000
,
0
.
592
,
0
.
612
}
\definecolor{lst-arduino-dblq}{rgb}{
0
.
000
,
0
.
361
,
0
.
373
}
\definecolor{lst-arduino-literal1}{rgb}{
0
.
000
,
0
.
592
,
0
.
612
} %
BLUE
\definecolor{lst-arduino-keyword1}{rgb}{
0
.
827
,
0
.
329
,
0
.
000
} %
ORANGE
\definecolor{lst-arduino-keyword2}{rgb}{
0
.
827
,
0
.
329
,
0
.
000
} %
ORANGE
\definecolor{lst-arduino-keyword3}{rgb}{
0
.
447
,
0
.
557
,
0
.
000
} %
GREEN
\definecolor{lst-arduino-directive}{rgb}{
0
.
369
,
0
.
427
,
0
.
012
}
\definecolor{lst-arduino-commentc}{rgb}{
0
.
584
,
0
.
647
,
0
.
651
}
\definecolor{lst-arduino-commentcpp}{rgb}{
0
.
263
,
0
.
310
,
0
.
329
}
\definecolor{lst-arduino-identifier}{rgb}{
0
.
000
,
0
.
000
,
0
.
000
}
\lstdefinelanguage{Arduino}
{
showspaces=
false
,
showstringspaces=
false
,
showtabs=
false
,
morestring=[s][\color{lst-arduino-sngq}]{
'}{'
},
morestring=[s][\color{lst-arduino-dblq}]{
"}{"
},
keywordstyle=[
1
]{\color{lst-arduino-literal1}},%
LITERAL1
(
BLUE
)
keywordstyle=[
2
]{\color{lst-arduino-keyword1}\bfseries},%
KEYWORD1
(
ORANGE
)
keywordstyle=[
3
]{\color{lst-arduino-keyword2}},%
KEYWORD2
(
ORANGE
)
keywordstyle=[
4
]{\color{lst-arduino-keyword3}},%
KEYWORD3
(
GREEN
)
directivestyle={\color{lst-arduino-directive}},
morecomment=[s][\color{lst-arduino-commentc}]{/*}{*/},
morecomment=[l][\color{lst-arduino-commentcpp}]{//},
identifierstyle={\color{lst-arduino-identifier}},
EOS
header_str.
sub(/
__GENERATED_FROM__
/,
@ref_dirs
.map{|x|
"% Generated from: #{x}\n"
}.join).
sub(/
__KEYWORDS_TXTS__
/,
@keywords_txts
.map{|x|
"% #{x}\n"
}.join)
end
def
footer
<<~
'EOS'
moredelim=*[directive]\
moredirectives={
define,elif,
else
,endif,error,
if
,ifdef,ifndef,line,%
include,pragma,
undef
,warning
},
sensitive=
true
}
EOS
end
def
middle
tex_lines = []
%w(
LITERAL1
KEYWORD1
KEYWORD2
KEYWORD3
).
each
.with_index(
1
)
do
|k, i|
tex_lines <<
"morekeywords=[#{i}]{"
tex_lines <<
" %%% #{k}"
ary =
@tokens
[k].each_slice(
5
).to_a
ary.take(ary.size-
1
).map{|a|
" "
+ a.join(
","
) +
","
}.
each
do
|l|
tex_lines << l
end
tex_lines <<
" "
+ ary.last.join(
","
)
tex_lines <<
"},"
end
tex_lines.map{|s|
" #{s}\n"
}.join
end
def
to_listings
header + middle + footer
end
end
print ArduinoIDEParser.
new
(
"C:/Program Files (x86)/Arduino"
,
"C:/Users/foo/AppData/Local/Arduino15/packages"
,
"C:/Users/foo/Documents/Arduino/libraries"
).to_listings