def create_generator(source_file_name=None, generator_name=None): """ Make a generator from the given information about the object, such as its source and type """ if generator_name: return LanguageInfo.find_generator_by_name(generator_name)() else: if source_file_name: try: h = LanguageInfo.guess_language_for_file(source_file_name) return h.get_default_html_generator()() except IOError: # this means silvercity tried to the file and failed raise ValueError, "Unknown extension for '%s', cannot create lexer" % source_file_name else: raise ValueError, "Unknown file type, cannot create lexer"
def handle_form(template_name): template = file(os.path.join(template_path, template_name), 'r').read() generators = LanguageInfo.get_generator_names_descriptions() generators.sort() template = template % { 'template' : template_name, 'generators' : '\n'.join(['<option value="%s">%s</option>' % (name, description) for name, description in generators]), 'encodings' : '\n'.join(['<option value="%s">%s</option>' % (value, name) for value, name in encodings])} print template
def guess_language(url, content): if url is not None: ext = url.split('.')[-1] extension_guesses = LanguageInfo.guess_languages_for_extension(ext) else: extension_guesses = [] if len(extension_guesses) == 1: return extension_guesses[0] else: shebang = content.split('\n')[0] shebang_guesses = LanguageInfo.guess_languages_for_shebang(shebang) if len(extension_guesses) > 0: guesses = [eg for eg in extension_guesses if eg in shebang_guesses] else: guesses = shebang_guesses if len(guesses) == 1: return guesses[0] from SilverCity import NULL return NULL.null_language_info
def generate_html( source_file_name=None, # Use clipboard, hackish way of specifying target_file_name=None, css='', generator=None, title='', view=0, clean=0, prefix=xhtml_prefix, suffix=suffix): if target_file_name is None: if view: target_file_name = tempfile.mktemp('.html') target_file = open(target_file_name, 'w') else: target_file = sys.stdout else: target_file = open(target_file_name, 'w') if not css: css = 'file:' + urllib.pathname2url( SilverCity.get_default_stylesheet_location()) if source_file_name is not None: generator = create_generator(source_file_name, generator) target_file.write(xhtml_prefix % { 'title': title or os.path.basename(source_file_name), 'css': css }) source = open(source_file_name, 'r').read() else: source = get_clipboard() if generator is not None: generator = LanguageInfo.find_generator_by_name(generator)() else: language = guess_language_for_buffer(source) generator = language.get_default_html_generator()() if clean: source = clean_source(source) generator.generate_html(target_file, source) target_file.write(suffix) if view: target_file.close() webbrowser.open(target_file_name)
def generate_html( source_file_name = None, # Use clipboard, hackish way of specifying target_file_name = None, css = '', generator = None, title = '', view = 0, clean = 0, prefix = xhtml_prefix, suffix = suffix): if target_file_name is None: if view: target_file_name = tempfile.mktemp('.html') target_file = open(target_file_name, 'w') else: target_file = sys.stdout else: target_file = open(target_file_name, 'w') if not css: css = 'file:' + urllib.request.pathname2url( SilverCity.get_default_stylesheet_location()) if source_file_name is not None: generator = create_generator(source_file_name, generator) target_file.write(xhtml_prefix % { 'title' : title or os.path.basename(source_file_name), 'css' : css}) source = open(source_file_name, 'r').read() else: source = get_clipboard() if generator is not None: generator = LanguageInfo.find_generator_by_name(generator)() else: language = guess_language_for_buffer(source) generator = language.get_default_html_generator()() if clean: source = clean_source(source) generator.generate_html(target_file, source) target_file.write(suffix) if view: target_file.close() webbrowser.open(target_file_name)
def handle_form(template_name): template = file(os.path.join(template_path, template_name), 'r').read() generators = LanguageInfo.get_generator_names_descriptions() generators.sort() template = template % { 'template': template_name, 'generators': '\n'.join([ '<option value="%s">%s</option>' % (name, description) for name, description in generators ]), 'encodings': '\n'.join([ '<option value="%s">%s</option>' % (value, name) for value, name in encodings ]) } print template
def create_generator(source_url, generator_name, content): if generator_name: return LanguageInfo.find_generator_by_name(generator_name)() else: return guess_language(source_url, content).get_default_html_generator()()
def create_generator(source_file_name, generator_name): if generator_name: return LanguageInfo.find_generator_by_name(generator_name)() else: return LanguageInfo.guess_language_for_file( source_file_name).get_default_html_generator()()
generator.generate_html(target_file, source) target_file.write(suffix) if view: target_file.close() webbrowser.open(target_file_name) if __name__ == "__main__": args = sys.argv[1:] if (args == 0) or ("--help" in args) or ("-help" in args): print(usage) sys.exit(0) switches, other_args = collect_options(args) if 'list-generators' in switches: lexers = LanguageInfo.get_generator_names_descriptions() lexers.sort() print("\nGenerator Options:") for name, description in lexers: print(" --generator=%s (%s)" % (name, description)) print() sys.exit(0) if 'clip' in switches: html_args = {} del switches['clip'] elif len(other_args) > 0: html_args = {'source_file_name' : other_args[0]} other_args = other_args[1:] else: print(usage)
target_file.write(suffix) if view: target_file.close() webbrowser.open(target_file_name) if __name__ == "__main__": args = sys.argv[1:] if (args == 0) or ("--help" in args) or ("-help" in args): print usage sys.exit(0) switches, other_args = collect_options(args) if switches.has_key('list-generators'): lexers = LanguageInfo.get_generator_names_descriptions() lexers.sort() print "\nGenerator Options:" for name, description in lexers: print " --generator=%s (%s)" % (name, description) print sys.exit(0) if switches.has_key('clip'): html_args = {} del switches['clip'] elif len(other_args) > 0: html_args = {'source_file_name': other_args[0]} other_args = other_args[1:] else: print usage
def list_generators(): """ This returns a list of generators, a generator is a valid language, so these are things like perl, python, xml etc...""" lexers = LanguageInfo.get_generator_names_descriptions() return lexers
# $Id: source.py,v 1.3 2003/12/10 01:25:48 zopezen Exp $ # Copyright: ClearWind Consulting Ltd from SilverCity import LanguageInfo from StringIO import StringIO # add in extra extensions LanguageInfo.add_extension("python", "cpy") LanguageInfo.add_extension("python", "vpy") LanguageInfo.add_extension("html", "pt") LanguageInfo.add_extension("html", "cpt") def create_generator(source_file_name=None, generator_name=None): """ Make a generator from the given information about the object, such as its source and type """ if generator_name: return LanguageInfo.find_generator_by_name(generator_name)() else: if source_file_name: try: h = LanguageInfo.guess_language_for_file(source_file_name) return h.get_default_html_generator()() except IOError: # this means silvercity tried to the file and failed raise ValueError, "Unknown extension for '%s', cannot create lexer" % source_file_name else: raise ValueError, "Unknown file type, cannot create lexer"