<commit_before>#!/usr/bin/env python3

#####################################
# Module Imports
import codecs, os.path,re, subprocess

from sphinx.builders import Builder
from sphinx.util.osutil import ensuredir, os_path
from sphinx.util.console import bold, darkgreen
from sphinx.util.nodes import inline_all_toctrees
from sphinx.util.compat import Directive

from docutils import nodes, writers
from docutils.io import StringOutput


class BacchBuilder(Builder):
    
    name = "bacch"
    format = "latex"
    extension = ".tex"
    writer = None

    def init(self):
        pass

    def get_outdated_docs(self):
        return 'all documents'

    def prepare_writing(self,docnames):
        self.writer = BacchWriter(self.config)
    
    def get_relative_uri(self,from_,to,typ=None):
        return self.get_target_uri(to,typ)

    def get_target_uri(self,docname,typ):
        return '%' + docname

    def write(self, *ignored):
        docnames = self.env.all_docs

        self.info(bold("Preparing documents..."),nonl=True)
        self.prepare_writing(docnames)
        self.info("Done")

        self.info(bold("Assembling single document..."),nonl=True)
        doctree = self.assemble_doctree()
        self.info("Done")
        self.info()
        self.info(bold("Writing..."),nonl=True)
        self.write_doc(self.config.master_doc,doctree)
        self.outfile = os.path.join(
            self.outdir,os_path(
                self.config.master_doc) + self.extension)
        self.docfile = os.path.join(
            self.outdir,os_path(self.config.master_doc) + '.pdf')

        ensuredir(os.path.dirname(self.outfile))
        self.write_file(self.outfile,self.writer.output)
        self.info("Done")

    def assemble_doctree(self):
        master = self.config.master_doc
        tree = self.env.get_doctree(master)
        tree = inline_all_toctrees(self,set(),master,tree,darkgreen)
        tree['docname'] = master
        
        return tree

    def write_doc(self,docname,doctree):
        destination = StringOutput(encoding = 'utf-8')
        output = self.writer.write(doctree,destination)

    def write_file(self,outfile,content):
        try:
            f = codecs.open(outfile,'w','utf-8')
            try:
                f.write(content)
            finally:
                f.close()
        except (IOError,OSError) as err:
            self.warn("Error writing file %s: %s" % (outfile, err))

    def finish(self):

        # Convert LaTeX File into PDF
        command = ['pdflatex',
                   '-output-directory',
                   self.outdir,
                   self.outfile]
        subprocess.call(command)

        # Move New File from build dir 
        title = self.config.bacch_title.lower().replace(' ','') + '.pdf'
        newfile = self.srcdir + '/../' + title
        basepdf = self.config.master_doc + '.pdf'<commit_msg>Fixes to bacch_sphinx.<commit_after>#!/usr/bin/env python3

#####################################
# Module Imports
import codecs, os.path,re, subprocess

from sphinx.builders import Builder
from sphinx.util.osutil import ensuredir, os_path
from sphinx.util.console import bold, darkgreen
from sphinx.util.nodes import inline_all_toctrees
from sphinx.util.compat import Directive

from docutils import nodes, writers
from docutils.io import StringOutput


class BacchBuilder(Builder):
    
    name = "bacch"
    format = "latex"
    extension = ".tex"
    writer = None

    def init(self):
        pass

    def get_outdated_docs(self):
        return 'all documents'

    def prepare_writing(self,docnames):
        self.writer = BacchWriter(self.config)
    
    def get_relative_uri(self,from_,to,typ=None):
        return self.get_target_uri(to,typ)

    def get_target_uri(self,docname,typ):
        return '%' + docname

    def write(self, *ignored):
        docnames = self.env.all_docs

        self.info(bold("Preparing documents..."),nonl=True)
        self.prepare_writing(docnames)
        self.info("Done")

        self.info(bold("Assembling single document..."),nonl=True)
        doctree = self.assemble_doctree()
        self.info("Done")
        self.info()
        self.info(bold("Writing..."),nonl=True)
        self.write_doc(self.config.master_doc,doctree)
        self.outfile = os.path.join(
            self.outdir,os_path(
                self.config.master_doc) + self.extension)
        self.docfile = os.path.join(
            self.outdir,os_path(self.config.master_doc) + '.pdf')

        ensuredir(os.path.dirname(self.outfile))
        self.write_file(self.outfile,self.writer.output)
        self.info("Done")

    def assemble_doctree(self):
        master = self.config.master_doc
        tree = self.env.get_doctree(master)
        tree = inline_all_toctrees(self,set(),master,tree,darkgreen)
        tree['docname'] = master
        
        return tree

    def write_doc(self,docname,doctree):
        destination = StringOutput(encoding = 'utf-8')
        output = self.writer.write(doctree,destination)

    def write_file(self,outfile,content):
        try:
            f = codecs.open(outfile,'w','utf-8')
            try:
                f.write(content)
            finally:
                f.close()
        except (IOError,OSError) as err:
            self.warn("Error writing file %s: %s" % (outfile, err))

    def finish(self):

        # Convert LaTeX File into PDF
        command = ['pdflatex',
                   '-output-directory',
                   self.outdir,
                   self.outfile]
        subprocess.call(command)

        # Move New File from build dir 
        title = self.config.bacch_title.lower().replace(' ','') + '.pdf'
        newfile = self.srcdir + '/../' + title
        basepdf = self.outdir + '/' + self.config.master_doc + '.pdf'
{'fraction_non_alphanumeric': 0.13001549853624936, 'fraction_numerical': 0.0010332357499569485, 'mean_word_length': 2.3846153846153846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4402459', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1774, 'n_words': 334}