Use new utils module for colors in asciidoc2html.

This commit is contained in:
Florian Bruhin 2014-09-18 07:44:04 +02:00
parent 0ce54ec1fc
commit c9a24f32f5
2 changed files with 81 additions and 6 deletions

View File

@ -21,10 +21,13 @@
"""Generate the html documentation based on the asciidoc files."""
import os
import sys
import subprocess
import glob
import colorama as col
sys.path.insert(0, os.getcwd())
from scripts import utils
def call_asciidoc(src, dst):
@ -34,8 +37,8 @@ def call_asciidoc(src, dst):
src: The source .asciidoc file.
dst: The destination .html file, or None to auto-guess.
"""
print("{}Calling asciidoc for {}...{}".format(
col.Fore.CYAN, os.path.basename(src), col.Fore.RESET))
utils.print_col("Calling asciidoc for {}...".format(
os.path.basename(src)), 'cyan')
if os.name == 'nt':
# FIXME this is highly specific to my machine
args = [r'C:\Python27\python', r'J:\bin\asciidoc-8.6.9\asciidoc.py']
@ -47,11 +50,12 @@ def call_asciidoc(src, dst):
try:
subprocess.check_call(args)
except subprocess.CalledProcessError as e:
print(''.join([col.Fore.RED, str(e), col.Fore.RESET]))
utils.print_col(str(e), 'red')
sys.exit(1)
def main():
def main(colors=False):
utils.use_color = colors
asciidoc_files = [
('doc/qutebrowser.1.asciidoc', None),
('README.asciidoc', None),
@ -70,4 +74,4 @@ def main():
if __name__ == '__main__':
main()
main(colors=True)

71
scripts/utils.py Normal file
View File

@ -0,0 +1,71 @@
#!/usr/bin/python3 # vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Utility functions for scripts."""
use_color = True
fg_colors = {
'black': 30,
'red': 31,
'green': 32,
'yellow': 33,
'blue': 34,
'magenta': 35,
'cyan': 36,
'white': 37,
'reset': 39,
}
bg_colors = {name: col + 10 for name, col in fg_colors.items()}
term_attributes = {
'bright': 1,
'dim': 2,
'normal': 22,
'reset': 0,
}
def _esc(code):
return '\033[{}m'.format(code)
def print_col(text, color):
"""Print a colorized text."""
if use_color:
fg = _esc(fg_colors[color.lower()])
reset = _esc(fg_colors['reset'])
print(''.join([fg, text, reset]))
else:
print(text)
def print_bold(text):
"""Print a bold text."""
if use_color:
bold = _esc(term_attributes['bright'])
reset = _esc(term_attributes['reset'])
print(''.join([bold, text, reset]))
else:
print(text)