def publish(obj, path, ext=None, linkify=None, index=None, **kwargs): """Publish an object to a given format. The function can be called in two ways: 1. document or item-like object + output file path 2. tree-like object + output directory path :param obj: (1) Item, list of Items, Document or (2) Tree :param path: (1) output file path or (2) output directory path :param ext: file extension to override output extension :param linkify: turn links into hyperlinks (for Markdown or HTML) :param index: create an index.html (for HTML) :raises: :class:`doorstop.common.DoorstopError` for unknown file formats :return: output location if files created, else None """ # Determine the output format ext = ext or os.path.splitext(path)[-1] or '.html' check(ext) if linkify is None: linkify = is_tree(obj) and ext == '.html' if index is None: index = is_tree(obj) and ext == '.html' # Publish documents count = 0 for obj2, path2 in iter_documents(obj, path, ext): count += 1 # Publish content to the specified path common.create_dirname(path2) log.info("publishing to {}...".format(path2)) lines = publish_lines(obj2, ext, linkify=linkify, **kwargs) common.write_lines(lines, path2) # Create index if index and count: _index(path, tree=obj if is_tree(obj) else None) # Return the published path if count: msg = "published to {} file{}".format(count, 's' if count > 1 else '') log.info(msg) return path else: log.warning("nothing to publish") return None """ copy the CSS file to the output directory so we can reference it instead of embedding it """ if ext == '.html': shutil.copy(CSS, path)
def publish(obj, path, ext=None, linkify=None, index=None, **kwargs): """Publish an object to a given format. The function can be called in two ways: 1. document or item-like object + output file path 2. tree-like object + output directory path :param obj: (1) Item, list of Items, Document or (2) Tree :param path: (1) output file path or (2) output directory path :param ext: file extension to override output extension :param linkify: turn links into hyperlinks (for Markdown or HTML) :param index: create an index.html (for HTML) :raises: :class:`doorstop.common.DoorstopError` for unknown file formats :return: output location if files created, else None """ # Determine the output format ext = ext or os.path.splitext(path)[-1] or '.html' check(ext) if linkify is None: linkify = is_tree(obj) and ext in ['.html', '.md'] if index is None: index = is_tree(obj) and ext == '.html' # Publish documents count = 0 for obj2, path2 in iter_documents(obj, path, ext): count += 1 # Publish content to the specified path common.create_dirname(path2) log.info("publishing to {}...".format(path2)) lines = publish_lines(obj2, ext, linkify=linkify, **kwargs) common.write_lines(lines, path2) if obj2.assets: src = obj2.assets dst = os.path.join(os.path.dirname(path2), obj2.ASSETS) common.copy(src, dst) # Create index if index and count: _index(path, tree=obj if is_tree(obj) else None) # Return the published path if count: msg = "published to {} file{}".format(count, 's' if count > 1 else '') log.info(msg) return path else: log.warning("nothing to publish") return None
def export(obj, path, ext=None, **kwargs): """Export an object to a given format. The function can be called in two ways: 1. document or item-like object + output file path 2. tree-like object + output directory path :param obj: (1) Item, list of Items, Document or (2) Tree :param path: (1) output file path or (2) output directory path :param ext: file extension to override output extension :raises: :class:`doorstop.common.DoorstopError` for unknown file formats :return: output location if files created, else None """ # Determine the output format ext = ext or os.path.splitext(path)[-1] or '.csv' check(ext) # Export documents count = 0 for obj2, path2 in iter_documents(obj, path, ext): count += 1 # Export content to the specified path common.create_dirname(path2) log.info("exporting to {}...".format(path2)) if ext in FORMAT_LINES: lines = export_lines(obj2, ext, **kwargs) common.write_lines(lines, path2) else: export_file(obj2, path2, ext, **kwargs) # Return the exported path if count: msg = "exported to {} file{}".format(count, 's' if count > 1 else '') log.info(msg) return path else: log.warning("nothing to export") return None
def _index(directory, index=INDEX, extensions=('.html',), tree=None): """Create an HTML index of all files in a directory. :param directory: directory for index :param index: filename for index :param extensions: file extensions to include :param tree: optional tree to determine index structure """ # Get paths for the index index filenames = [] for filename in os.listdir(directory): if filename.endswith(extensions) and filename != INDEX: filenames.append(os.path.join(filename)) # Create the index if filenames: path = os.path.join(directory, index) log.info("creating an {}...".format(index)) lines = _lines_index(filenames, tree=tree) common.write_lines(lines, path) else: log.warning("no files for {}".format(index))
def index(self, value): """Create or update the document's index.""" if value: path = os.path.join(self.path, Document.INDEX) log.info("creating {} index...".format(self)) common.write_lines(self._lines_index(self.items), path)
def publish(obj, path, ext=None, linkify=None, index=None, template=None, toc=True, **kwargs): """Publish an object to a given format. The function can be called in two ways: 1. document or item-like object + output file path 2. tree-like object + output directory path :param obj: (1) Item, list of Items, Document or (2) Tree :param path: (1) output file path or (2) output directory path :param ext: file extension to override output extension :param linkify: turn links into hyperlinks (for Markdown or HTML) :param index: create an index.html (for HTML) :raises: :class:`doorstop.common.DoorstopError` for unknown file formats :return: output location if files created, else None """ # Determine the output format ext = ext or os.path.splitext(path)[-1] or '.html' check(ext) if linkify is None: linkify = is_tree(obj) and ext in ['.html', '.md'] if index is None: index = is_tree(obj) and ext == '.html' if is_tree(obj): assets_dir = os.path.join(path, Document.ASSETS) # path is a directory name else: assets_dir = os.path.join(os.path.dirname(path), Document.ASSETS) # path is a filename if os.path.isdir(assets_dir): log.info('Deleting contents of assets directory %s', assets_dir) common.delete_contents(assets_dir) else: os.makedirs(assets_dir) # If publish html and then markdown ensure that the html template are still available if not template: template = HTMLTEMPLATE template_assets = os.path.join(os.path.dirname(__file__), 'files', 'assets') if os.path.isdir(template_assets): log.info("Copying %s to %s", template_assets, assets_dir) common.copy_dir_contents(template_assets, assets_dir) # Publish documents count = 0 for obj2, path2 in iter_documents(obj, path, ext): count += 1 # Publish content to the specified path log.info("publishing to {}...".format(path2)) lines = publish_lines(obj2, ext, linkify=linkify, template=template, toc=toc, **kwargs) common.write_lines(lines, path2) if obj2.copy_assets(assets_dir): log.info('Copied assets from %s to %s', obj.assets, assets_dir) # Create index if index and count: _index(path, tree=obj if is_tree(obj) else None) # Return the published path if count: msg = "published to {} file{}".format(count, 's' if count > 1 else '') log.info(msg) return path else: log.warning("nothing to publish") return None
def publish( obj, path, ext=None, linkify=None, index=None, template=None, toc=True, **kwargs ): """Publish an object to a given format. The function can be called in two ways: 1. document or item-like object + output file path 2. tree-like object + output directory path :param obj: (1) Item, list of Items, Document or (2) Tree :param path: (1) output file path or (2) output directory path :param ext: file extension to override output extension :param linkify: turn links into hyperlinks (for Markdown or HTML) :param index: create an index.html (for HTML) :raises: :class:`doorstop.common.DoorstopError` for unknown file formats :return: output location if files created, else None """ # Determine the output format ext = ext or os.path.splitext(path)[-1] or '.html' check(ext) if linkify is None: linkify = is_tree(obj) and ext in ['.html', '.md'] if index is None: index = is_tree(obj) and ext == '.html' if is_tree(obj): assets_dir = os.path.join(path, Document.ASSETS) # path is a directory name else: assets_dir = os.path.join( os.path.dirname(path), Document.ASSETS ) # path is a filename if os.path.isdir(assets_dir): log.info('Deleting contents of assets directory %s', assets_dir) common.delete_contents(assets_dir) else: os.makedirs(assets_dir) # If publish html and then markdown ensure that the html template are still available if not template: template = HTMLTEMPLATE template_assets = os.path.join(os.path.dirname(__file__), 'files', 'assets') if os.path.isdir(template_assets): log.info("Copying %s to %s", template_assets, assets_dir) common.copy_dir_contents(template_assets, assets_dir) # Publish documents count = 0 for obj2, path2 in iter_documents(obj, path, ext): count += 1 # Publish content to the specified path log.info("publishing to {}...".format(path2)) lines = publish_lines( obj2, ext, linkify=linkify, template=template, toc=toc, **kwargs ) common.write_lines(lines, path2) if obj2.copy_assets(assets_dir): log.info('Copied assets from %s to %s', obj.assets, assets_dir) # Create index if index and count: _index(path, tree=obj if is_tree(obj) else None) # Return the published path if count: msg = "published to {} file{}".format(count, 's' if count > 1 else '') log.info(msg) return path else: log.warning("nothing to publish") return None