Refactor asciidoc2html.py.
This commit is contained in:
parent
bd189392a8
commit
71a150af22
@ -32,86 +32,47 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir))
|
|||||||
from scripts import utils
|
from scripts import utils
|
||||||
|
|
||||||
|
|
||||||
def _get_asciidoc_cmd(args):
|
class AsciiDoc:
|
||||||
"""Try to find out what commandline to use to invoke asciidoc."""
|
|
||||||
if args.asciidoc is not None:
|
|
||||||
return args.asciidoc
|
|
||||||
|
|
||||||
try:
|
"""Abstraction of an asciidoc subprocess."""
|
||||||
subprocess.call(['asciidoc'], stdout=subprocess.DEVNULL,
|
|
||||||
stderr=subprocess.DEVNULL)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
return ['asciidoc']
|
|
||||||
|
|
||||||
try:
|
FILES = [
|
||||||
subprocess.call(['asciidoc.py'], stdout=subprocess.DEVNULL,
|
|
||||||
stderr=subprocess.DEVNULL)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
return ['asciidoc.py']
|
|
||||||
|
|
||||||
raise FileNotFoundError
|
|
||||||
|
|
||||||
|
|
||||||
def call_asciidoc(args, src, dst):
|
|
||||||
"""Call asciidoc for the given files.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
args: The asciidoc binary to use, as a list.
|
|
||||||
src: The source .asciidoc file.
|
|
||||||
dst: The destination .html file, or None to auto-guess.
|
|
||||||
"""
|
|
||||||
print("Calling asciidoc for {}...".format(os.path.basename(src)))
|
|
||||||
args = args[:]
|
|
||||||
if dst is not None:
|
|
||||||
args += ['--out-file', dst]
|
|
||||||
args.append(src)
|
|
||||||
try:
|
|
||||||
subprocess.check_call(args)
|
|
||||||
except (subprocess.CalledProcessError, OSError) as e:
|
|
||||||
utils.print_col(str(e), 'red')
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def main(colors=False):
|
|
||||||
"""Generate html files for the online documentation."""
|
|
||||||
utils.change_cwd()
|
|
||||||
utils.use_color = colors
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument('--all', help="Build all documentation into a given "
|
|
||||||
"directory.", nargs=1)
|
|
||||||
parser.add_argument('--asciidoc', help="Full path to python and "
|
|
||||||
"asciidoc.py. If not given, it's searched in PATH.",
|
|
||||||
nargs=2, required=False,
|
|
||||||
metavar=('PYTHON', 'ASCIIDOC'))
|
|
||||||
args = parser.parse_args()
|
|
||||||
asciidoc_files = [
|
|
||||||
('FAQ.asciidoc', 'qutebrowser/html/doc/FAQ.html'),
|
('FAQ.asciidoc', 'qutebrowser/html/doc/FAQ.html'),
|
||||||
('CHANGELOG.asciidoc', 'qutebrowser/html/doc/CHANGELOG.html'),
|
('CHANGELOG.asciidoc', 'qutebrowser/html/doc/CHANGELOG.html'),
|
||||||
('doc/quickstart.asciidoc', 'qutebrowser/html/doc/quickstart.html'),
|
('doc/quickstart.asciidoc', 'qutebrowser/html/doc/quickstart.html'),
|
||||||
('doc/userscripts.asciidoc', 'qutebrowser/html/doc/userscripts.html'),
|
('doc/userscripts.asciidoc', 'qutebrowser/html/doc/userscripts.html'),
|
||||||
]
|
]
|
||||||
try:
|
|
||||||
os.mkdir('qutebrowser/html/doc')
|
def __init__(self, args):
|
||||||
except FileExistsError:
|
self._cmd = None
|
||||||
pass
|
self._args = args
|
||||||
try:
|
|
||||||
asciidoc = _get_asciidoc_cmd(args)
|
def prepare(self):
|
||||||
except FileNotFoundError:
|
"""Get the asciidoc command and create the homedir to use."""
|
||||||
utils.print_col("Could not find asciidoc! Please install it, or use "
|
self._cmd = self._get_asciidoc_cmd()
|
||||||
"the --asciidoc argument to point this script to the "
|
|
||||||
"correct python/asciidoc.py location!", 'red')
|
def build(self):
|
||||||
sys.exit(1)
|
if self._args.website:
|
||||||
if args.all:
|
self._build_website()
|
||||||
|
else:
|
||||||
|
self._build_docs()
|
||||||
|
|
||||||
|
def _build_docs(self):
|
||||||
|
files = self.FILES[:]
|
||||||
|
for src in glob.glob('doc/help/*.asciidoc'):
|
||||||
|
name, _ext = os.path.splitext(os.path.basename(src))
|
||||||
|
dst = 'qutebrowser/html/doc/{}.html'.format(name)
|
||||||
|
files.append((src, dst))
|
||||||
|
for src, dst in files:
|
||||||
|
self.call(src, dst)
|
||||||
|
|
||||||
|
def _build_website(self):
|
||||||
for root, _dirs, files in os.walk(os.getcwd()):
|
for root, _dirs, files in os.walk(os.getcwd()):
|
||||||
for filename in files:
|
for filename in files:
|
||||||
if os.path.splitext(filename)[1] != '.asciidoc':
|
if os.path.splitext(filename)[1] != '.asciidoc':
|
||||||
continue
|
continue
|
||||||
src = os.path.join(root, filename)
|
src = os.path.join(root, filename)
|
||||||
parts = [args.all[0]]
|
parts = [self._args.all[0]]
|
||||||
dirname = os.path.dirname(src)
|
dirname = os.path.dirname(src)
|
||||||
if dirname:
|
if dirname:
|
||||||
parts.append(os.path.relpath(os.path.dirname(src)))
|
parts.append(os.path.relpath(os.path.dirname(src)))
|
||||||
@ -120,14 +81,77 @@ def main(colors=False):
|
|||||||
'html')))
|
'html')))
|
||||||
dst = os.path.join(*parts)
|
dst = os.path.join(*parts)
|
||||||
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
||||||
call_asciidoc(asciidoc, src, dst)
|
self.call(src, dst)
|
||||||
else:
|
|
||||||
for src in glob.glob('doc/help/*.asciidoc'):
|
def _get_asciidoc_cmd(self):
|
||||||
name, _ext = os.path.splitext(os.path.basename(src))
|
"""Try to find out what commandline to use to invoke asciidoc."""
|
||||||
dst = 'qutebrowser/html/doc/{}.html'.format(name)
|
if self._args.asciidoc is not None:
|
||||||
asciidoc_files.append((src, dst))
|
return self._args.asciidoc
|
||||||
for src, dst in asciidoc_files:
|
|
||||||
call_asciidoc(asciidoc, src, dst)
|
try:
|
||||||
|
subprocess.call(['asciidoc'], stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.DEVNULL)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return ['asciidoc']
|
||||||
|
|
||||||
|
try:
|
||||||
|
subprocess.call(['asciidoc.py'], stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.DEVNULL)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return ['asciidoc.py']
|
||||||
|
|
||||||
|
raise FileNotFoundError
|
||||||
|
|
||||||
|
def call(self, src, dst, theme=None):
|
||||||
|
"""Call asciidoc for the given files.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
src: The source .asciidoc file.
|
||||||
|
dst: The destination .html file, or None to auto-guess.
|
||||||
|
theme: The CSS to use as a theme.
|
||||||
|
"""
|
||||||
|
print("Calling asciidoc for {}...".format(os.path.basename(src)))
|
||||||
|
args = self._cmd[:]
|
||||||
|
if dst is not None:
|
||||||
|
args += ['--out-file', dst]
|
||||||
|
args.append(src)
|
||||||
|
try:
|
||||||
|
subprocess.check_call(args)
|
||||||
|
except (subprocess.CalledProcessError, OSError) as e:
|
||||||
|
utils.print_col(str(e), 'red')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def main(colors=False):
|
||||||
|
"""Generate html files for the online documentation."""
|
||||||
|
utils.change_cwd()
|
||||||
|
utils.use_color = colors
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--website', help="Build website into a given "
|
||||||
|
"directory.", nargs=1)
|
||||||
|
parser.add_argument('--asciidoc', help="Full path to python and "
|
||||||
|
"asciidoc.py. If not given, it's searched in PATH.",
|
||||||
|
nargs=2, required=False,
|
||||||
|
metavar=('PYTHON', 'ASCIIDOC'))
|
||||||
|
args = parser.parse_args()
|
||||||
|
try:
|
||||||
|
os.mkdir('qutebrowser/html/doc')
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
asciidoc = AsciiDoc(args)
|
||||||
|
try:
|
||||||
|
asciidoc.prepare()
|
||||||
|
except FileNotFoundError:
|
||||||
|
utils.print_col("Could not find asciidoc! Please install it, or use "
|
||||||
|
"the --asciidoc argument to point this script to the "
|
||||||
|
"correct python/asciidoc.py location!", 'red')
|
||||||
|
sys.exit(1)
|
||||||
|
asciidoc.build()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user