Skip to content
Permalink
Browse files

Add compiler friendly names

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jun 21, 2015
1 parent b4ba52a commit 8c6e235118a4023994a084de7a2711f73d5b352b
@@ -231,7 +231,8 @@ def process(self, task):
class PageCompiler(BasePlugin):
"""Plugins that compile text files into HTML."""

name = "dummy compiler"
name = "dummy_compiler"
friendly_name = ''
demote_headers = False
supports_onefile = True
default_metadata = {
@@ -44,28 +44,23 @@
LOGGER = POSTLOGGER


def filter_post_pages(compiler, is_post, compilers, post_pages, compiler_names, compilers_raw=None):
def filter_post_pages(compiler, is_post, compilers, post_pages, compiler_objs, compilers_raw):
"""Given a compiler ("markdown", "rest"), and whether it's meant for
a post or a page, and compilers, return the correct entry from
post_pages."""

# compilers_raw is used only for passing to print_compilers, but we can fill
# in the old version ifneeded
if compilers_raw is None:
compilers_raw = compilers

# First throw away all the post_pages with the wrong is_post
filtered = [entry for entry in post_pages if entry[3] == is_post]

# These are the extensions supported by the required format
extensions = compilers.get(compiler)
if extensions is None:
if compiler in compiler_names:
if compiler in compiler_objs:
LOGGER.error("There is a {0} compiler available, but it's not set in your COMPILERS option.".format(compiler))
LOGGER.info("Read more: {0}".format(COMPILERS_DOC_LINK))
else:
LOGGER.error('Unknown format {0}'.format(compiler))
print_compilers(compilers_raw, post_pages, compiler_names)
print_compilers(compilers_raw, post_pages, compiler_objs)
sys.exit(1)

# Throw away the post_pages with the wrong extensions
@@ -84,32 +79,33 @@ def filter_post_pages(compiler, is_post, compilers, post_pages, compiler_names,
return filtered[0]


def print_compilers(compilers_raw, post_pages, compiler_names):
def print_compilers(compilers_raw, post_pages, compiler_objs):
"""
List all available compilers in a human-friendly format.
:param compilers_raw: The compilers dict, mapping compiler names to tuples of extensions
:param post_pages: The post_pages structure
:param compiler_names: Names of all installed compiler plugins
:param compilers_objs: Compiler objects
"""

# We use compilers_raw, because the normal dict can contain
# garbage coming from the translation candidate implementation.
# Entries are in format: (name, extensions, used_in_post_pages)
parsed_compilers = {'used': [], 'unused': [], 'disabled': []}

for compiler_name in compiler_names:
for compiler_name, compiler_obj in compiler_objs.items():
fname = compiler_obj.friendly_name or compiler_name
if compiler_name not in compilers_raw:
parsed_compilers['disabled'].append((compiler_name, (), False))
parsed_compilers['disabled'].append((compiler_name, fname, (), False))
else:
# stolen from filter_post_pages
extensions = compilers_raw[compiler_name]
filtered = [entry for entry in post_pages if any(
[ext in entry[0] for ext in extensions])]
if filtered:
parsed_compilers['used'].append((compiler_name, extensions, True))
parsed_compilers['used'].append((compiler_name, fname, extensions, True))
else:
parsed_compilers['unused'].append((compiler_name, extensions, False))
parsed_compilers['unused'].append((compiler_name, fname, extensions, False))

# Sort compilers alphabetically by name, just so it’s prettier (and
# deterministic)
@@ -122,17 +118,18 @@ def print_compilers(compilers_raw, post_pages, compiler_names):

print("Available input formats:\n")

width = max([len(i[0]) for i in parsed_list])
name_width = max([len(i[0]) for i in parsed_list] + [4]) # 4 == len('NAME')
fname_width = max([len(i[1]) for i in parsed_list] + [11]) # 11 == len('DESCRIPTION')

print((' {0:<' + str(width) + '} EXTENSIONS\n').format('NAME'))
print((' {0:<' + str(name_width) + '} {1:<' + str(fname_width) + '} EXTENSIONS\n').format('NAME', 'DESCRIPTION'))

for name, extensions, used in parsed_list:
for name, fname, extensions, used in parsed_list:
flag = ' ' if used else '!'
flag = flag if extensions else '~'

extensions = ', '.join(extensions) if extensions else '(disabled: not in COMPILERS)'

print(('{flag}{name:<' + str(width) + '} {extensions}').format(flag=flag, name=name, extensions=extensions))
print(('{flag}{name:<' + str(name_width) + '} {fname:<' + str(fname_width) + '} {extensions}').format(flag=flag, name=name, fname=fname, extensions=extensions))

print("""
More compilers are available in the Plugins Index.
@@ -336,7 +333,7 @@ def _execute(self, options, args):
wants_available = options['available-formats']

if wants_available:
print_compilers(self.site.config['_COMPILERS_RAW'], self.site.config['post_pages'], compiler_names)
print_compilers(self.site.config['_COMPILERS_RAW'], self.site.config['post_pages'], self.site.compilers)
return

if is_page:
@@ -363,7 +360,7 @@ def _execute(self, options, args):

if content_format not in compiler_names:
LOGGER.error("Unknown {0} format {1}, maybe you need to install a plugin?".format(content_type, content_format))
print_compilers(self.site.config['_COMPILERS_RAW'], self.site.config['post_pages'], compiler_names)
print_compilers(self.site.config['_COMPILERS_RAW'], self.site.config['post_pages'], self.site.compilers)
return
compiler_plugin = self.site.plugin_manager.getPluginByName(
content_format, "PageCompiler").plugin_object
@@ -372,7 +369,7 @@ def _execute(self, options, args):
entry = filter_post_pages(content_format, is_post,
self.site.config['COMPILERS'],
self.site.config['post_pages'],
compiler_names,
self.site.compilers,
self.site.config['_COMPILERS_RAW'])

if import_file:
@@ -38,6 +38,7 @@
class CompileHtml(PageCompiler):
"""Compile HTML into HTML."""
name = "html"
friendly_name = "HTML"

def compile_html(self, source, dest, is_two_file=True):
makedirs(os.path.dirname(dest))
@@ -56,6 +56,7 @@ class CompileIPynb(PageCompiler):
"""Compile IPynb into HTML."""

name = "ipynb"
friendly_name = "Jupyter/IPython Notebook"
demote_headers = True
default_kernel = 'python2' if sys.version_info[0] == 2 else 'python3'

@@ -44,9 +44,10 @@


class CompileMarkdown(PageCompiler):
"""Compile markdown into HTML."""
"""Compile Markdown into HTML."""

name = "markdown"
friendly_name = "Markdown"
demote_headers = True
extensions = []
site = None
@@ -44,6 +44,7 @@ class CompilePandoc(PageCompiler):
"""Compile markups into HTML using pandoc."""

name = "pandoc"
friendly_name = "pandoc"

def set_site(self, site):
self.config_dependencies = [str(site.config['PANDOC_OPTIONS'])]
@@ -40,6 +40,7 @@ class CompilePhp(PageCompiler):
"""Compile PHP into PHP."""

name = "php"
friendly_name = "PHP"

def compile_html(self, source, dest, is_two_file=True):
makedirs(os.path.dirname(dest))
@@ -40,9 +40,10 @@


class CompileRest(PageCompiler):
"""Compile reSt into HTML."""
"""Compile reStructuredText into HTML."""

name = "rest"
friendly_name = "reStructuredText"
demote_headers = True
logger = None

0 comments on commit 8c6e235

Please sign in to comment.
You can’t perform that action at this time.