def __init__(self, project, name): self._project = project self._name = name self._dir = os.path.join(self._project.getConfig(["dirs", "layouts"]), name) self._other_files = [] self._files = {} self._files["template"] = os.path.join(self._dir, "template.html") self._files["entry"] = os.path.join(self._dir, "blog", "entry.html") self._files["separator"] = os.path.join(self._dir, "blog", "separator.html") self._files["begin"] = os.path.join(self._dir, "blog", "begin.html") self._files["end"] = os.path.join(self._dir, "blog", "end.html") self._files["singleentry"] = os.path.join(self._dir, "blog", "singleentry.html") self._templates = {} print("Found layout: " + self._name) # Check if template files exist and read them for i in self._files: try: self._templates[i] = open(self._files[i]).read() except IOError as e: fail('Error reading "' + self._files[i] + '": ' + str(e)) # Search other files (CSS, images, ...) for f in findFiles(self._dir, [".html"]): print("\tFound file: " + f) self._other_files.append(OtherFile(self._dir, os.path.relpath(f, self._dir)))
def __init__(self, project_dir="", test=False, output=None): self._sites = [] self._layouts = {} self._root_dir = project_dir self._test = test self._other_output = output self._config = Config(os.path.join(self._root_dir, "stawebg.json"), Config.global_struct) # Make directories absolute dirs = self._config.get(["dirs"]) for k in dirs: dirs[k] = os.path.join(self._root_dir, dirs[k]) # Set locale try: locale.setlocale(locale.LC_ALL, self.getConfig(["locale"], False, "")) except locale.Error as e: fail('Failed to set the locale "' + self.getConfig(["locale"], False, "") + '": ' + str(e)) # Add all layouts to list for name in listFolders(self.getConfig(["dirs", "layouts"])): self._layouts[name] = Layout(self, name) # Add all site directories to list for s in listFolders(self.getConfig(["dirs", "sites"])): site = Site(s, self) self._sites.append(site) site.read() # copy files to out dir for s in self._sites: s.copy()
def createOutput(self, dest, text): # TODO: move to helper.py?, use for other files too? mkdir(os.path.dirname(dest)) try: outf = open(dest, "w") outf.write(text) outf.close() except IOError as e: # TODO: check exceptions fail("Error creating " + dest + ": " + str(e))
def getLayout(self, name=None): if not name: name = "default" layout = self._layouts.get(name) if not layout: fail("Can't find layout: " + name) return layout
def read(self): # read site specific config filename = os.path.join(self.getConfig(["dirs", "sites"]), self._name + ".json") if not os.path.isfile(filename): fail("Can't find config file: " + filename) site_config = Config(filename, Config.site_struct) self._config = Config.merge(self._config, site_config, True) # create file index path = self.getAbsDestPath() if os.path.isdir(path): self._file_index = findFiles(path) # read all pages self._readHelper(self.getAbsSrcPath(), self._root)
def _translateMarkup(self, src, ext=None): text = "" # src is string -> file extension given if ext: text = src # src if filename else: if not src: return text with open(src, "rt") as f: text = f.read() ext = os.path.splitext(src)[1] config = self._project.getConfig(["markup"], False) if not config: return text tool = config.get(ext) if tool: try: p = Popen(tool, stdin=PIPE, stdout=PIPE, stderr=PIPE) except PermissionError as e: fail(" ".join(tool) + ": " + str(e)) except FileNotFoundError as e: fail(" ".join(tool) + ": " + str(e)) out, err = p.communicate(text.encode()) if p.returncode: fail(" ".join(tool) + ": " + err.decode()) if len(err): print("Warning from " + " ".join(tool) + ": " + err.decode()) return out.decode() return text