def input_dir(self, config=None): """ Full input dir path for specified config. """ if config is None: config = self.config return fs.real(fs.join(self.path, config.get('input_path', '.')))
def load_tree(cls, path, config, nav_path='', parent=None): """ Load chapters tree recursivelly. """ index_name = config.get('index', cls.INDEX_FILE) files, dirs = [], [] # print ("Load tree: %s, %s" % (path, config)) # scan sub-dirs and files for name in os.listdir(path): full_path = fs.join(path, name) # dirs if fs.isdir(full_path): if not cls.is_name_masked(name): dirs.append(full_path) # files elif fs.isfile(full_path): if cls.is_file_to_render(name): files.append(name) # no index = no data here, but don't give up, try scan deeper! if not index_name in files: cls.load_subdirs(dirs, config, nav_path=nav_path, parent=parent) return # index found = create chapter! initial_title = fs.basename(path).capitalize() chapter = cls(config, title=initial_title, nav_path=nav_path, is_index=True) chapter.load_meta(file_path=fs.join(path, index_name)) files.remove(index_name) # add to parent if parent: parent.add_child(chapter) # load files cls.load_files(path, files, config, nav_path=nav_path, parent=chapter) # load dirs cls.load_subdirs(dirs, config, nav_path=nav_path, parent=chapter) # print ("chapter: %s, files: %s, dirs: %s" % (str(chapter), files, dirs)) return chapter
def load_files(cls, base_path, files, config, nav_path='', parent=None): """ Helper for loading chapters from files. """ for name in files: file_path = fs.join(base_path, name) file_slug = cls.slug_by_name(name) file_nav_path = nav_path and '/'.join((nav_path, file_slug)) or file_slug child = cls(config, title=file_slug.capitalize(), nav_path=file_nav_path, is_index=False) child.load_meta(file_path) parent.add_child(child)
def cmd_init(self): import docta # Test if dir is empty for name in os.listdir(self.current_dir()): if not name.startswith('.'): command = ' '.join((self.script_name(), self.args.command)) exit_with_error("Current dir is not empty. Please run `%s` in empty dir." % command) # Copy initial project source_dir = fs.real(fs.dirname(docta.__file__)) initial_dir = fs.join(source_dir, 'initial', 'default') fs.cp(initial_dir, self.current_dir())
def render_chapter(self, chapter, home=False): # print("Render: %s" % str(chapter)) output_dir = self.project.output_dir(self.out_format) # dir for index # if chapter.is_index: # # print(fs.path_for_dir(output_dir, chapter.rel_dir_path)) # fs.mkdirs(fs.path_for_dir(output_dir, chapter.rel_dir_path)) # load content - render - flush content chapter.load_content() if not chapter.content_raw is None: out_file_path = fs.join(output_dir, chapter.rel_dir_path, self.get_html_name(chapter)) fs.mkdirs(fs.dirname(out_file_path)) # print(out_file_path) if 'template' in chapter.meta: template = self.get_template(chapter.meta['template']) elif home: template = self.get_template('home.html') elif chapter.is_index: template = self.get_template('index.html') else: template = self.get_template('page.html') with fs.open(out_file_path, 'w') as out_file: context = self.template_context(chapter) html = template.render(**context) out_file.write(html) chapter.flush_content() # render children for child in chapter.children: self.render_chapter(child)
def current_config(self): if not hasattr(self, '_config'): config_path = fs.join(self.current_dir(), self.args.config) config_format = (self.args.config.rsplit('.', 1)[-1]).lower() try: config_file = fs.open(config_path, 'r') except: raise Exception("can't load config file: %s" % config_path) if config_format == 'json': # JSON config try: self._config = json.load(config_file) except Exception as e: raise Exception("bad JSON format in config! %s" % log.exc_to_str(e)) else: # try YAML by default try: self._config = yaml.load(config_file) except Exception as e: raise Exception("bad YAML format in config %s" % log.exc_to_str(e)) config_file.close() return self._config