def run(self, package=None, file=None, prompt=False): package = package or current_help_package(window=self.window) if package is None or prompt: return help_package_prompt(help_index_list(), on_select=lambda p: self.run(p, file)) pkg_info = help_index_list().get(package, None) if pkg_info is None: return log("Cannot edit help file; package '%s' unknown", package, dialog=True) files = pkg_info.help_files items = [[key, files[key]] for key in files] if not items: return log("The help index for '%s' lists no help files", package, dialog=True) if file is not None: return open_local_help(pkg_info, file, window=self.window) def pick(index): if index >= 0: open_local_help(pkg_info, items[index][0], window=self.window) self.window.show_quick_panel(items=items, on_select=lambda index: pick(index))
def run(self, edit): filename = self.filename() if filename is None: return log("Cannot reload help index; not in package", status=True) filename = os.path.relpath(filename, sublime.packages_path()) package = os.path.split(filename)[0].split(os.sep)[0] # If package is missing, force a complete rescan. if package not in help_index_list(): package = None help_index_list(reload=True, package=package)
def run(self, package=None, prompt=False): package = package or current_help_package(window=self.window) if package is None or prompt: return help_package_prompt(help_index_list(), on_select=lambda pkg: self.run(pkg)) pkg_info = help_index_list().get(package, None) if pkg_info is None: return log("Cannot edit help file; package '%s' unknown", package, dialog=True) open_help_index(pkg_info)
def _global_package_list(filter_with_help=True): """ Yield a list of packages that Sublime currently knows about (those that have at least one resource and are not ignored), optionally filtering away packages that have help indexes defined currently. The list is yielded in package load order. """ pkg_set = set() for res in sublime.find_resources(''): if res.startswith("Packages/"): pkg_set.add(res.split("/")[1]) if filter_with_help: pkg_set -= set([pkg.package for pkg in help_index_list().values()]) if "Default" in pkg_set: yield "Default" for pkg in sorted(pkg_set): if pkg not in ["Default", "User"]: yield pkg if "User" in pkg_set: yield "User"
def run(self, package=None, doc_root=None): if package is None: items = list(_global_package_list()) def pick(i): if i >= 0: self.run(items[i], doc_root) return self.window.show_quick_panel(items=items, on_select=lambda i: pick(i)) if package in help_index_list(): return _error_dialog( """ Specified package already has help defined: '%s' Use the Edit Help Index command to edit the existing help system in this package. """, package) if doc_root is not None: return self.create_index(package, doc_root) self.window.show_input_panel("Document Root: Packages/%s/" % package, "help/", lambda r: self.create_index(package, r), None, None)
def create_file(self, package, file): if not file: return log("No help file given; skipping creation", status=True) pkg_info = help_index_list().get(package) local_path = local_help_filename(pkg_info, file) help_file = os.path.split(local_path) os.makedirs(help_file[0], exist_ok=True) view = self.window.new_file() view.settings().set("_hh_auth", True) view.settings().set("default_dir", help_file[0]) view.set_name(help_file[1]) apply_authoring_settings(view) template = format_template( """ %%hyperhelp title="${1:Title}" date="${2:%s}" $0 """, datetime.date.today().strftime("%Y-%m-%d")) view.run_command("insert_snippet", {"contents": template})
def run(self, package=None, file=None, prompt=False): package = package or current_help_package(window=self.window) if package is None or prompt: return help_package_prompt(help_index_list(), on_select=lambda p: self.run(p, file)) if help_index_list().get(package, None) is None: return log("Cannot add help file; package '%s' unknown", package, dialog=True) if file is not None: return self.create_file(package, file) self.window.show_input_panel( "New Help File (%s)" % package, "", lambda file: self.create_file(package, file), None, None)
def can_lint_view(view): """ Determine if the provided view can be the source of a lint. To be valid the view must represent a hyperhelp data file that has a path rooted in the Packages folder inside of a package whose help index is known. """ if (view is not None and view.file_name() is not None and view.file_name().startswith(sublime.packages_path()) and view.match_selector(0, "text.hyperhelp")): name = os.path.relpath(view.file_name(), sublime.packages_path()) pkg_name = name[:name.index(os.sep)] return pkg_name in help_index_list() return False
def run(self, edit): topic = None if self.view.match_selector(0, "source.python"): extract = self.view.sel()[0] if extract.empty(): extract = self.view.expand_by_class( extract, sublime.CLASS_WORD_START | sublime.CLASS_WORD_END) topic = self.view.substr(extract) pkg_info = help_index_list().get("SnAPI") if lookup_help_topic(pkg_info, topic): return sublime.run_command("hyperhelp_topic", { "package": "SnAPI", "topic": topic }) sublime.run_command("hyperhelp_index", {"package": "SnAPI"}) if topic is not None: self.view.window().run_command("insert", {"characters": topic})
def find_lint_target(view): """ Examine a given view and return a LintTarget that describes what is being linted. None is returned if the view is not a valid lint target. """ if not can_lint_view(view): return None name = view.file_name() parts = os.path.relpath(name, sublime.packages_path()).split(os.sep) pkg_name = parts[0] target = parts[-1] pkg_info = help_index_list().get(pkg_name) if view.match_selector(0, "text.hyperhelp.help"): return LintTarget("single", pkg_info, [target]) return LintTarget("package", pkg_info, list(pkg_info.help_files))
def is_enabled(self): return "SnAPI" in help_index_list()
def reload(self, help_view, help_file): if reload_help_file(help_index_list(), help_view): log("Reloaded help file '%s'", help_file, status=True)
def create_index(self, package, doc_root): root_path = self.make_document_root(package, doc_root) if root_path is not None: index_path = os.path.join(root_path, "hyperhelp.json") help_path = os.path.join(root_path, "index.txt") if os.path.exists(index_path): return _error_dialog( """ Help index file already exists in package: '%s' This may indicate that the index is broken and cannot be loaded. """, package) if os.path.exists(help_path): return _error_dialog( """ Root help file already exists in package: '%s' This may indicate that an existing help index for this package is broken and cannot be loaded. """, package) try: os.makedirs(root_path, exist_ok=True) _make_help_index(package, doc_root, index_path) _make_root_help(package, help_path) res = "Packages/%s/%shyperhelp.json" % (package, doc_root) new_pkg_info = load_help_index(res) if new_pkg_info is None: raise IOError("Unable to load new help index") help_index_list()[package] = new_pkg_info msg = format_template( """ Initial help files created for package: '%s' -> %s/hyperhelp.json -> %s/index.txt """, package, doc_root, doc_root) # Prompt the user to see if they want to open the files just # created or not. if sublime.ok_cancel_dialog(msg, "Open created files"): self.window.run_command("hyperhelp_author_edit_index", {"package": package}) self.window.run_command("hyperhelp_author_edit_help", { "package": package, "file": "index.txt" }) # This relies on load_resource() being able to load a # resource that find_resources() can't find yet; might # need to make help loading open local files as for # indexes. sublime.run_command("hyperhelp_topic", { "package": package, "topic": "index.txt" }) except: return _error_dialog( """ Error adding help to package: '%s' Unable to create the document root, index file or root help file. """, package)