SublimeText / LaTeXTools

LaTeX plugin for Sublime Text
https://latextools.readthedocs.io/
2.01k stars 365 forks source link

Cannot find my custom builder when build #1386

Closed TonyWu20 closed 5 years ago

TonyWu20 commented 5 years ago

I had been using a custom builder before an update of LaTeXTools in April, 2016. After that update and until now, that custom builder is not useable because every time I build, the Sublime Text 2/3 shows the message: "Cannot find builder advanced. Check your LaTeXTools Preferences." (advanced is the name of the custom builder). The custom builder file name is "advancedBuilder.py", and I set the builder option in LaTeXTools.sublime-settings as "advanced". This worked in the version before April, 2016, and then it doesn't work now. I have tried putting the builder file into the User/builders directory, still it doesn't work. The console message shows it goes to the directory, but cannot find the builder. This is in fact a long-existing issue. I didn't find any help in previous discussions, and the author of this builder said he stopped using sublime text for LaTeX so he couldn't provide any maintenance on this builder. I uploaded the builder file for your reference. advancedBuilder.py.zip

ig0774 commented 5 years ago

I make no guarantees that this will work, but here is a version that should at least be found:

from __future__ import print_function

import sublime

import re
import codecs

from pdfBuilder import PdfBuilder

DEBUG = False

DEFAULT_COMMAND_SIMPLE = [
    "%E", "-interaction=nonstopmode -synctex=1", "OPTION"]

DEFAULT_COMMAND_LATEXMK = [
    "latexmk",
    "-cd", "-e",
    "$pdflatex = '%E -interaction=nonstopmode -synctex=1 OPTION %S %O'",
    "-f", "-pdf"]

DEFAULT_COMMAND_WINDOWS_MIKTEX = [
    "texify", "-b", "-p",
    "--tex-option=\"--synctex=1\"", "OPTION"]

class AdvancedBuilder(PdfBuilder):
    def __init__(self, tex_root, output, builder_settings, platform_settings):
        # Sets the file name parts, plus internal stuff
        super(AdvancedBuilder, self).__init__(tex_root, output, builder_settings, platform_settings)
        self.name = "Advanced Builder"
        # Display output?
        self.display_log = builder_settings.get("display_log", False)
        self.engine = 'pdflatex'
        self.option = ""

    def commands(self):
        # print greeting
        self.display("\n\n")
        self.display("This is the Advanced Builder.")
        self.display("\n")
        # copy vars
        engine = self.engine
        option = self.option
        tex_name = self.tex_name
        builder_name = "SIMPLE"

        # check root file, settings in the comments
        for line in codecs.open(self.tex_root, "r", "UTF-8", "ignore").readlines():
            if not line.startswith("%"):
                # if not comment line, break
                break
            else:
                # process the comment lines
                mEng = re.match(r"%\s*![Tt][Ee][Xx]\s+(?:TS-)?program *= *(xelatex|lualatex|pdflatex|xetex|luatex|pdftex|platex-ng|ptex-ng)\s*$", line)
                mOpt = re.match(r"%\s*![Tt][Ee][Xx]\s+(?:TS-)?option *= *(.*)\s*?$", line)
                mBui = re.match(r"%\s*![Tt][Ee][Xx]\s+(?:TS-)?builder *= *([Ss][Ii][Mm][Pp][Ll][Ee]|[Ll][Aa][Tt][Ee][Xx][Mm][Kk]|[Tt][Ee][Xx][Ii][Ff][Yy])\s*$", line)
                # The engine
                if mEng:  # is not None
                    engine = mEng.group(1)
                # The option
                if mOpt:  # is not None
                    option = mOpt.group(1)
                if mBui:
                    builder_name = mBui.group(1).upper()
        if builder_name == "LATEXMK":
            cmd = DEFAULT_COMMAND_LATEXMK[:]
        elif builder_name == "TEXIFY":
            cmd = DEFAULT_COMMAND_WINDOWS_MIKTEX[:]
        else:
            cmd = DEFAULT_COMMAND_SIMPLE[:]

        # apply the settings
        if 'latexmk' == cmd[0]:
            cmd[3] = cmd[3].replace("%E", engine).replace("OPTION", option)
        elif 'texify' == cmd[0]:
            if engine != self.engine:
                sublime.error_message("Sorry, cannot select engine using a %!TEX program directive on MikTeX.\n")
                yield("", "Could not compile.")
            if option != self.option:
                cmd[4] = " ".join(["--tex-option=\"%s\"" % opt for opt in option.split(" ")])
            else:
                cmd[4] = ""
        else:  # SIMPLE
            cmd[0] = engine
            cmd[2] = option
        # print the whole command
        self.display("Command Line:\n")
        for element in cmd + [tex_name]:
            self.display(element + " ")
        else:
            self.display("\n")
        # texify wants the .tex extension; latexmk doesn't care either way
        yield (cmd + [tex_name], "")
        self.display("Done.\n")

        # This is for debugging purposes
        if self.display_log:
            self.display("\nCommand results:\n")
            self.display(self.out)
            self.display("\n\n")

Note that all the features of this builder, apart from support for a !TEX builder = ... directive are currently implemented by the default LaTeXTools builder for recent versions.

TonyWu20 commented 5 years ago

Thank you so much for the help! I will test it later. By the way, for "the default LaTeXTools builder', are you referring to the traditionalBuilder or else in the LaTeXTools?

ig0774 commented 5 years ago

Yes, I meant the traditional builder.