def convert(txt, target, headers=None, options=None): """Perform the conversion of a given txt2tags ttext to a specific target.""" # Here is the marked body text, it must be a list. txt = txt.split('\n') # Perform custom preproc txt = custom_preproc(txt, target) # Base configuration config = _get_config(target) # Set the three header fields if headers is None: headers = ['', '', ''] config['header1'] = headers[0] config['header2'] = headers[1] config['header3'] = headers[2] config['target'] = target if options is not None: if options.get('preproc'): options['preproc'].extend(config['preproc']) if options.get('postproc'): options['postproc'].extend(config['postproc']) config.update(options) # Check sanity of the configuration config = txt2tags.ConfigMaster().sanity(config) # Let's do the conversion try: # Convert target_body, marked_toc = txt2tags.convert(txt, config) # Footer #target_foot = txt2tags.doFooter(config) #target_foot.pop() # Unneded because we use txt2tags as a module target_foot = [] # Table of content tagged_toc = txt2tags.toc_tagger(marked_toc, config) target_toc = txt2tags.toc_formatter(tagged_toc, config) target_body = txt2tags.toc_inside_body(target_body, target_toc, config) if not txt2tags.AUTOTOC and not config['toc-only']: target_toc = [] # Full body config['fullBody'] = target_toc + target_body + target_foot # Headers outlist = txt2tags.doHeader(headers, config) # End document finished = txt2tags.finish_him(outlist, config) result = '\n'.join(finished) # Txt2tags error, show the messsage to the user except txt2tags.error, msg: logging.error(msg) result = msg
def t2t(f): infiles_config = txt2tags.get_infiles_config([f])[0] infiles_config[0]['infile'] = infiles_config[0]['sourcefile'] infiles_config[0]['toc_tagger'] = 0 source_head, source_conf, source_body = infiles_config[1] myconf = txt2tags.ConfigMaster().sanity(infiles_config[0]) target_head = txt2tags.doHeader(source_head, myconf) first_body_line = (len(source_head) or 1) + len(source_conf) + 1 target_body, marked_toc = txt2tags.convert(source_body, myconf, firstlinenr=first_body_line) tagged_toc = txt2tags.toc_tagger(marked_toc, myconf) target_toc = txt2tags.toc_formatter(tagged_toc, myconf) target_body = txt2tags.toc_inside_body(target_body, target_toc, myconf) target_foot = txt2tags.doFooter(myconf) outlist = target_head + target_toc + target_body + target_foot return '\n'.join(outlist)
# from __future__ import print_function # Remember to place the 'txt2tags.py' file on the same dir import txt2tags # Here is the marked body text, it must be a list. txt = "=Hi!=\nHave a **nice** day.\n\nBye." txt = txt.split("\n") # Set the three header fields headers = ["Header 1", "Header 2", "Header 3"] # Set the configuration on the 'config' dict. config = txt2tags.ConfigMaster()._get_defaults() config["outfile"] = txt2tags.MODULEOUT # results as list config["target"] = "html" # target type: HTML config["encoding"] = "UTF-8" # document encoding config["css-sugar"] = 1 # CSS friendly config["toc"] = 1 # show Table Of Contents # The Pre (and Post) processing config is a list of lists: # [ [this, that], [foo, bar], [pattern, replace] ] config["preproc"] = [] config["preproc"].append(["nice", "VERY NICE"]) config["preproc"].append(["day", "life"]) # Let's do the conversion try: headers = txt2tags.doHeader(headers, config)
def _get_config(type): """Get the Txt2Tags configuration for specific target.""" # Set the configuration on the 'config' dict. config = txt2tags.ConfigMaster()._get_defaults() config['sourcefile'] = txt2tags.MODULEIN config['currentsourcefile'] = txt2tags.MODULEIN config['infile'] = txt2tags.MODULEIN config['outfile'] = txt2tags.MODULEOUT # results as list # The Pre (and Post) processing config is a list of lists: # [ [this, that], [foo, bar], [patt, replace] ] config['postproc'] = [] config['preproc'] = [] if type == 'xhtml' or type == 'xhtmls' or type == 'html' or type == 'html5': # Default values config['encoding'] = 'UTF-8' config['toc'] = 0 config['style'] = ['themes/default.css'] config['css-inside'] = 0 config['css-sugar'] = 1 # Remove target comments config['preproc'].append(['%xhtmls% ', '']) config['preproc'].append(['%html% ', '']) # Alias # Allow line breaks, r'\\\\' are 2 \ for regexes #config['preproc'].append([r'\\\\', '\'\'<br />\'\'']) # F*ck, these are used in Latex :S config['preproc'].append([r'@@', '\'\'<br />\'\'' ]) # F*ck, @@ is used in diffs :S # Math and code blocks are commented because txt2tags doesn't support # newlines in preproc, see: # http://code.google.com/p/txt2tags/issues/detail?id=25 # For now, custom_preproc() is used ## Support math block #config['preproc'].append(['<<<', '\'\'\'\n<pre class="math">']) #config['preproc'].append(['>>>', '</pre>\n\'\'\'']) ## Support code block #config['preproc'].append(['{{{ (' + '|'.join(supported_languages.keys()) + ')', '\'\'\'\n<pre class="brush: \\1; class-name: code;">']) #config['preproc'].append(['}}}', '</pre>\n\'\'\'']) # Semantic tags, in case user use visual tags config['postproc'].append([r'(?i)(</?)b>', r'\1strong>']) config['postproc'].append([r'(?i)(</?)i>', r'\1em>']) config['postproc'].append([r'(?i)(</?)u>', r'\1ins>']) config['postproc'].append([r'(?i)(</?)s>', r'\1del>']) # Allow subscript superscript config['postproc'].append([r'\^\^(.*?)\^\^', r'<sup>\1</sup>']) config['postproc'].append([r',,(.*?),,', r'<sub>\1</sub>']) # Apply image resizing and correct path config['postproc'].append( [r'<img (.*?) src="(\d+)-', r'<img \1 width="\2" src="']) config['postproc'].append( [r'<img (.*?) src="', r'<img \1 src="media/images/']) elif type == 'tex': # Default values config['encoding'] = 'utf8' config['toc'] = 0 # Support math block config['preproc'].append(['<<<', '\'\'\'']) config['preproc'].append(['>>>', '\'\'\'']) # Remove target comments config['preproc'].append(['%tex% ', '']) config['preproc'].append(['%latex% ', '']) # Alias config['preproc'].append(['%pdf% ', '']) # Alias # Allow line breaks, r'\\\\' are 2 \ for regexes #config['preproc'].append([r'\$\\backslash\$\$\\backslash\$', r'\'\'\\\\\'\'']) # F*ck, these are used in Latex :S config['preproc'].append([r'@@', r"''\\newline{}''" ]) # F*ck, @@ is used in diffs :S ## Support code block #config['preproc'].append(['{{{ (' + '|'.join(supported_languages.keys()) + ')', '\'\'\'\n\\begin{verbatim}']) #config['preproc'].append(['}}}', '\\end{verbatim}\n\'\'\'\n']) # Allow subscript superscript config['postproc'].append( [r'\\\^{}\\\^{}(.*?)\\\^{}\\\^{}', r'\\superscript{\1}']) config['postproc'].append([r',,(.*?),,', r'\\subscript{\1}']) # Apply image resizing config['postproc'].append( [r'includegraphics\{(\d+)-', r'includegraphics[width=\1px]{']) config['postproc'].append([ r'includegraphics(.*?)\{', r'noindent\includegraphics\1{media/images/' ]) elif type == 'txt': # Default values config['toc'] = 0 # Remove target comments config['preproc'].append(['%txt% ', '']) config['preproc'].append(['%text% ', '']) # Alias # Allow line breaks, r'\\\\' are 2 \ for regexes #config['preproc'].append([r'\\\\', '\n']) # F*ck, these are used in Latex :S config['preproc'].append([r'@@', '\n']) # F*ck, @@ is used in diffs :S # Allow subscript superscript config['postproc'].append([r'\^\^(.*?)\^\^', r'\1']) config['postproc'].append([r',,(.*?),,', r'\1']) return config