Initial commit.

This commit is contained in:
2021-05-24 22:18:33 +03:00
commit e2954d55f4
3701 changed files with 330017 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
#
# AsciiDoc code filter configuration file.
#
# Documented in code-filter-readme.txt
#
[blockdef-listing]
code-style=template="listingblock",presubs=(),postsubs=("callouts",),posattrs=("style","language"),filter="code-filter.py -b {basebackend} -l {language}"

View File

@@ -0,0 +1,239 @@
#!/usr/libexec/platform-python
'''
NAME
code-filter - AsciiDoc filter to highlight language keywords
SYNOPSIS
code-filter -b backend -l language [ -t tabsize ]
[ --help | -h ] [ --version | -v ]
DESCRIPTION
This filter reads source code from the standard input, highlights language
keywords and comments and writes to the standard output.
The purpose of this program is to demonstrate how to write an AsciiDoc
filter -- it's much to simplistic to be passed off as a code syntax
highlighter. Use the 'source-highlight-filter' instead.
OPTIONS
--help, -h
Print this documentation.
-b
Backend output file format: 'docbook', 'linuxdoc', 'html', 'css'.
-l
The name of the source code language: 'python', 'ruby', 'c++', 'c'.
-t tabsize
Expand source tabs to tabsize spaces.
--version, -v
Print program version number.
BUGS
- Code on the same line as a block comment is treated as comment.
Keywords inside literal strings are highlighted.
- There doesn't appear to be an easy way to accomodate linuxdoc so
just pass it through without markup.
AUTHOR
Written by Stuart Rackham, <srackham@gmail.com>
URLS
http://sourceforge.net/projects/asciidoc/
http://asciidoc.org/
COPYING
Copyright (C) 2002-2006 Stuart Rackham. Free use of this software is
granted under the terms of the GNU General Public License (GPL).
'''
import os, sys, re
VERSION = '1.1.2'
# Globals.
language = None
backend = None
tabsize = 8
keywordtags = {
'html':
('<strong>','</strong>'),
'css':
('<strong>','</strong>'),
'docbook':
('<emphasis role="strong">','</emphasis>'),
'linuxdoc':
('','')
}
commenttags = {
'html':
('<i>','</i>'),
'css':
('<i>','</i>'),
'docbook':
('<emphasis>','</emphasis>'),
'linuxdoc':
('','')
}
keywords = {
'python':
('and', 'del', 'for', 'is', 'raise', 'assert', 'elif', 'from',
'lambda', 'return', 'break', 'else', 'global', 'not', 'try', 'class',
'except', 'if', 'or', 'while', 'continue', 'exec', 'import', 'pass',
'yield', 'def', 'finally', 'in', 'print'),
'ruby':
('__FILE__', 'and', 'def', 'end', 'in', 'or', 'self', 'unless',
'__LINE__', 'begin', 'defined?' 'ensure', 'module', 'redo', 'super',
'until', 'BEGIN', 'break', 'do', 'false', 'next', 'rescue', 'then',
'when', 'END', 'case', 'else', 'for', 'nil', 'retry', 'true', 'while',
'alias', 'class', 'elsif', 'if', 'not', 'return', 'undef', 'yield'),
'c++':
('asm', 'auto', 'bool', 'break', 'case', 'catch', 'char', 'class',
'const', 'const_cast', 'continue', 'default', 'delete', 'do', 'double',
'dynamic_cast', 'else', 'enum', 'explicit', 'export', 'extern',
'false', 'float', 'for', 'friend', 'goto', 'if', 'inline', 'int',
'long', 'mutable', 'namespace', 'new', 'operator', 'private',
'protected', 'public', 'register', 'reinterpret_cast', 'return',
'short', 'signed', 'sizeof', 'static', 'static_cast', 'struct',
'switch', 'template', 'this', 'throw', 'true', 'try', 'typedef',
'typeid', 'typename', 'union', 'unsigned', 'using', 'virtual', 'void',
'volatile', 'wchar_t', 'while')
}
block_comments = {
'python': ("'''","'''"),
'ruby': None,
'c++': ('/*','*/')
}
inline_comments = {
'python': '#',
'ruby': '#',
'c++': '//'
}
def print_stderr(line):
sys.stderr.write(line+os.linesep)
def sub_keyword(mo):
'''re.subs() argument to tag keywords.'''
word = mo.group('word')
if word in keywords[language]:
stag,etag = keywordtags[backend]
return stag+word+etag
else:
return word
def code_filter():
'''This function does all the work.'''
global language, backend
inline_comment = inline_comments[language]
blk_comment = block_comments[language]
if blk_comment:
blk_comment = (re.escape(block_comments[language][0]),
re.escape(block_comments[language][1]))
stag,etag = commenttags[backend]
in_comment = 0 # True if we're inside a multi-line block comment.
tag_comment = 0 # True if we should tag the current line as a comment.
line = sys.stdin.readline()
while line:
line = line.rstrip()
line = line.expandtabs(tabsize)
# Escape special characters.
line = line.replace('&','&amp;')
line = line.replace('<','&lt;')
line = line.replace('>','&gt;')
# Process block comment.
if blk_comment:
if in_comment:
if re.match(r'.*'+blk_comment[1]+r'$',line):
in_comment = 0
else:
if re.match(r'^\s*'+blk_comment[0]+r'.*'+blk_comment[1],line):
# Single line block comment.
tag_comment = 1
elif re.match(r'^\s*'+blk_comment[0],line):
# Start of multi-line block comment.
tag_comment = 1
in_comment = 1
else:
tag_comment = 0
if tag_comment:
if line: line = stag+line+etag
else:
if inline_comment:
pos = line.find(inline_comment)
else:
pos = -1
if pos >= 0:
# Process inline comment.
line = re.sub(r'\b(?P<word>\w+)\b',sub_keyword,line[:pos]) \
+ stag + line[pos:] + etag
else:
line = re.sub(r'\b(?P<word>\w+)\b',sub_keyword,line)
sys.stdout.write(line + os.linesep)
line = sys.stdin.readline()
def usage(msg=''):
if msg:
print_stderr(msg)
print_stderr('Usage: code-filter -b backend -l language [ -t tabsize ]')
print_stderr(' [ --help | -h ] [ --version | -v ]')
def main():
global language, backend, tabsize
# Process command line options.
import getopt
opts,args = getopt.getopt(sys.argv[1:],
'b:l:ht:v',
['help','version'])
if len(args) > 0:
usage()
sys.exit(1)
for o,v in opts:
if o in ('--help','-h'):
print(__doc__)
sys.exit(0)
if o in ('--version','-v'):
print('code-filter version %s' % (VERSION,))
sys.exit(0)
if o == '-b': backend = v
if o == '-l':
v = v.lower()
if v == 'c': v = 'c++'
language = v
if o == '-t':
try:
tabsize = int(v)
except:
usage('illegal tabsize')
sys.exit(1)
if tabsize <= 0:
usage('illegal tabsize')
sys.exit(1)
if backend is None:
usage('backend option is mandatory')
sys.exit(1)
if backend not in keywordtags:
usage('illegal backend option')
sys.exit(1)
if language is None:
usage('language option is mandatory')
sys.exit(1)
if language not in keywords:
usage('illegal language option')
sys.exit(1)
# Do the work.
code_filter()
if __name__ == "__main__":
try:
main()
except (KeyboardInterrupt, SystemExit):
pass
except:
print_stderr("%s: unexpected exit status: %s" %
(os.path.basename(sys.argv[0]), sys.exc_info()[1]))
# Exit with previous sys.exit() status or zero if no sys.exit().
sys.exit(sys.exc_info()[1])

View File

@@ -0,0 +1,53 @@
#
# AsciiDoc Graphviz filter configuration file.
#
# Version: 1.0
# Gouici Iisaka <iisaka51 at gmail dot com>
[graphviz-filter-style]
# When the filter output image is data-uri encoded write it to the indir
# (instead of the outdir) so that encoder can find it.
ifndef::data-uri[]
graphviz-style=template="graphviz{format?-{format}}-block",subs=(),posattrs=("style","target","layout","format"),filter='graphviz2png.py {verbose?-v} -o "{outdir={indir}}/{imagesdir=}{imagesdir?/}{target}" -L {layout=dot} -F {format=png} -'
endif::data-uri[]
ifdef::data-uri[]
graphviz-style=template="graphviz{format?-{format}}-block",subs=(),posattrs=("style","target","layout","format"),filter='graphviz2png.py {verbose?-v} -o "{indir={outdir}}/{imagesdir=}{imagesdir?/}{target}" -L {layout=dot} -F {format=png} -'
endif::data-uri[]
[blockdef-open]
template::[graphviz-filter-style]
[blockdef-listing]
template::[graphviz-filter-style]
[paradef-default]
template::[graphviz-filter-style]
[graphviz-block]
template::[filter-image-blockmacro]
# EXPERIMENTAL: xhtml11 backend SVG image block.
ifdef::basebackend-xhtml11[]
[graphviz-svg-block]
<div class="imageblock"{id? id="{id}"}{align? style="text-align:{align};"}{float? style="float:{float};"}>
<div class="content">
<a class="image" href="{link}">
<object data="{imagesdir=}{imagesdir?/}{target}" type="image/svg+xml" />
{link#}</a>
</div>
<div class="title">{caption={figure-caption} {counter:figure-number}. }{title}</div>
</div>
endif::basebackend-xhtml11[]
#
# DEPRECATED: Pre 8.2.7 filter definition.
#
[blockdef-graphviz]
delimiter=^graphviz~{4,}$
template=graphviz-block
presubs=none
filter=graphviz2png.py {verbose?-v} -o "{outdir={indir}}/{target}" -L {layout=dot} -
posattrs=target,format
#
# DEPRECATED: End
#

View File

@@ -0,0 +1,169 @@
#!/usr/libexec/platform-python
import os, sys, subprocess
from optparse import *
__AUTHOR__ = "Gouichi Iisaka <iisaka51@gmail.com>"
__VERSION__ = '1.1.4'
class EApp(Exception):
'''Application specific exception.'''
pass
class Application():
'''
NAME
graphviz2png - Converts textual graphviz notation to PNG file
SYNOPSIS
graphviz2png [options] INFILE
DESCRIPTION
This filter reads Graphviz notation text from the input file
INFILE (or stdin if INFILE is -), converts it to a PNG image file.
OPTIONS
-o OUTFILE, --outfile=OUTFILE
The file name of the output file. If not specified the output file is
named like INFILE but with a .png file name extension.
-L LAYOUT, --layout=LAYOUT
Graphviz layout: dot, neato, twopi, circo, fdp
Default is 'dot'.
-F FORMAT, --format=FORMAT
Graphviz output format: png, svg, or any other format Graphviz
supports. Run dot -T? to get the full list.
Default is 'png'.
-v, --verbose
Verbosely print processing information to stderr.
-h, --help
Print this documentation.
-V, --version
Print program version number.
SEE ALSO
graphviz(1)
AUTHOR
Written by Gouichi Iisaka, <iisaka51@gmail.com>
Format support added by Elmo Todurov, <todurov@gmail.com>
THANKS
Stuart Rackham, <srackham@gmail.com>
This script was inspired by his music2png.py and AsciiDoc
LICENSE
Copyright (C) 2008-2009 Gouichi Iisaka.
Free use of this software is granted under the terms of
the GNU General Public License (GPL).
'''
def __init__(self, argv=None):
# Run dot, get the list of supported formats. It's prefixed by some junk.
format_output = subprocess.Popen(["dot", "-T?"], stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[1].decode('utf-8')
# The junk contains : and ends with :. So we split it, then strip the final endline, then split the list for future usage.
supported_formats = format_output.split(": ")[2][:-1].split(" ")
if not argv:
argv = sys.argv
self.usage = '%prog [options] inputfile'
self.version = 'Version: %s\n' % __VERSION__
self.version += 'Copyright(c) 2008-2009: %s\n' % __AUTHOR__
self.option_list = [
Option("-o", "--outfile", action="store",
dest="outfile",
help="Output file"),
Option("-L", "--layout", action="store",
dest="layout", default="dot", type="choice",
choices=['dot','neato','twopi','circo','fdp'],
help="Layout type. LAYOUT=<dot|neato|twopi|circo|fdp>"),
Option("-F", "--format", action="store",
dest="format", default="png", type="choice",
choices=supported_formats,
help="Format type. FORMAT=<" + "|".join(supported_formats) + ">"),
Option("--debug", action="store_true",
dest="do_debug",
help=SUPPRESS_HELP),
Option("-v", "--verbose", action="store_true",
dest="do_verbose", default=False,
help="verbose output"),
]
self.parser = OptionParser( usage=self.usage, version=self.version,
option_list=self.option_list)
(self.options, self.args) = self.parser.parse_args()
if len(self.args) != 1:
self.parser.print_help()
sys.exit(1)
self.options.infile = self.args[0]
def systemcmd(self, cmd):
if self.options.do_verbose:
msg = 'Execute: %s' % cmd
sys.stderr.write(msg + os.linesep)
else:
cmd += ' 2>%s' % os.devnull
if os.system(cmd):
raise EApp('failed command: %s' % cmd)
def graphviz2png(self, infile, outfile):
'''Convert Graphviz notation in file infile to
PNG file named outfile.'''
outfile = os.path.abspath(outfile)
outdir = os.path.dirname(outfile)
if not os.path.isdir(outdir):
raise EApp('directory does not exist: %s' % outdir)
basefile = os.path.splitext(outfile)[0]
saved_cwd = os.getcwd()
os.chdir(outdir)
try:
cmd = '%s -T%s "%s" > "%s"' % (
self.options.layout, self.options.format, infile, outfile)
self.systemcmd(cmd)
finally:
os.chdir(saved_cwd)
if not self.options.do_debug:
os.unlink(infile)
def run(self):
if self.options.format == '':
self.options.format = 'png'
if self.options.infile == '-':
if self.options.outfile is None:
sys.stderr.write('OUTFILE must be specified')
sys.exit(1)
infile = os.path.splitext(self.options.outfile)[0] + '.txt'
lines = sys.stdin.readlines()
open(infile, 'w').writelines(lines)
if not os.path.isfile(infile):
raise EApp('input file does not exist: %s' % infile)
if self.options.outfile is None:
outfile = os.path.splitext(infile)[0] + '.png'
else:
outfile = self.options.outfile
self.graphviz2png(infile, outfile)
# To suppress asciidoc 'no output from filter' warnings.
if self.options.infile == '-':
sys.stdout.write(' ')
if __name__ == "__main__":
app = Application()
app.run()

View File

@@ -0,0 +1,140 @@
#
# AsciiDoc source code highlight filter configuration file.
#
# Documented in source-hightlight-filter.txt in AsciiDoc distribution
# ./examples/website/ directory.
#
# HTML outputs require GNU source-highlight (xhtml11, html4 outputs)
# http://www.gnu.org/software/src-highlite/source-highlight.html
#
# or Pygments (xhtml11 outputs):
# http://pygments.org/
#
# GNU source-hightlight is default, define the 'pygments' attribute to use
# Pygments.
#
########################
# Source block templates
########################
[source-highlight-block]
template::[listingblock]
ifdef::basebackend-html[]
[source-highlight-block]
<a name="{id}"></a>
<p><b>{title}</b></p>
<table{role? class="{role}"} border="0" bgcolor="#e8e8e8" width="100%" cellpadding="10"><tr><td>
{source-highlighter$highlight:}<pre><code>
|
{source-highlighter$highlight:}</code></pre>
</td></tr></table>
endif::basebackend-html[]
ifdef::basebackend-xhtml11,basebackend-html5[]
[source-highlight-block]
<div class="listingblock{role? {role}}">
<a id="{id}"></a>
<div class="title">{caption=}{title}</div>
<div class="content">
{source-highlighter$highlight:}<pre><code>
|
{source-highlighter$highlight:}</code></pre>
</div></div>
endif::basebackend-xhtml11,basebackend-html5[]
# Use DocBook programlisting element.
ifdef::basebackend-docbook[]
[source-highlight-block]
<formalpara{id? id="{id}"}{role? role="{role}"}{reftext? xreflabel="{reftext}"}><title>{title}</title><para>
{title#}<programlisting language="{language}" linenumbering="{src_numbered=unnumbered}"{args? {args}}>
{title%}<programlisting language="{language}"{role? role="{role}"} linenumbering="{src_numbered=unnumbered}"{args? {args}}>
|
</programlisting>
{title#}</para></formalpara>
endif::basebackend-docbook[]
# Source styles template.
ifdef::basebackend-html[]
[source-filter-style]
ifeval::["{source-highlighter}"=="source-highlight"]
source-style=template="source-highlight-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered","src_tab"),filter="source-highlight --gen-version -f xhtml -s {language} {src_numbered?--line-number=' '} {src_tab?--tab={src_tab}} {args=}"
endif::[]
ifeval::["{source-highlighter}"=="highlight"]
source-style=template="source-highlight-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered","src_tab"),filter="highlight --no-doc --inline-css --out-format=xhtml --syntax={language@python:py:{language}} {src_numbered?--line-number} {src_tab?--tab={src_tab}} --encoding={encoding} {args=}"
endif::[]
ifeval::["{source-highlighter}"=="pygments"]
source-style=template="source-highlight-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered"),filter="pygmentize -f html -l {language} {src_numbered?-O linenos=table} {encoding?-O encoding={encoding}} {args=}"
endif::[]
# DEPRECATED: 'pygments' attribute.
ifdef::pygments[]
source-style=template="source-highlight-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered"),filter="pygmentize -f html -l {language} {src_numbered?-O linenos=table} {encoding?-O encoding={encoding}} {args=}"
endif::[]
endif::basebackend-html[]
ifdef::basebackend-html4[]
[source-filter-style]
# html4 does not use pygments.
ifeval::["{source-highlighter}"=="source-highlight"]
source-style=template="source-highlight-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered","src_tab"),filter="source-highlight --gen-version -f html -s {language} {src_numbered?--line-number=' '} {src_tab?--tab={src_tab}} {args=}"
endif::[]
ifeval::["{source-highlighter}"=="highlight"]
source-style=template="source-highlight-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered","src_tab"),filter="highlight --no-doc --inline-css --out-format=html --syntax={language@python:py:{language}} {src_numbered?--line-number} {src_tab?--tab={src_tab}} {args=}"
endif::[]
endif::basebackend-html4[]
ifdef::basebackend-docbook[]
[source-filter-style]
source-style=template="source-highlight-block",presubs=(),postsubs=("specialcharacters","callouts"),posattrs=("style","language","src_numbered","src_tab")
endif::basebackend-docbook[]
#########################
# Source paragraph styles
#########################
[paradef-default]
template::[source-filter-style]
[paradef-literal]
template::[source-filter-style]
#########################
# Source block styles
#########################
[blockdef-open]
template::[source-filter-style]
[blockdef-listing]
template::[source-filter-style]
#
# DEPRECATED: Pre 8.2.7 filter definition.
#
#########################
# Source block definition
#########################
[blockdef-source-highlight]
# The old ^ delimiter is for backward compatibility, may be removed from
# in future versions.
delimiter=(^source~{4,}$)|(^\^{4,}$)
template=source-highlight-block
presubs=none
posattrs=language,src_numbered,src_tab
ifndef::basebackend-docbook[]
postsubs=callouts
# GNU Source Highlight filter.
filter=source-highlight -f {basebackend-xhtml11?xhtml}{basebackend-html4?html} -s {language} {src_numbered?--line-number} {src_tab?--tab={src_tab}}
endif::basebackend-docbook[]
ifdef::basebackend-docbook[]
postsubs=specialcharacters,callouts
# In the case of DocBook just pass the listing through and let the DocBook
# toolchain handle it.
filter=
endif::basebackend-docbook[]
#
# DEPRECATED: End
#