def img(img_path, classes=None, width=None, height=None, title=None, alt=None, link=None): from engineer.conf import settings template = settings.JINJA_ENV.get_template('theme/_img.html') classes = wrap_list(classes) rendered_content = template.render(source=img_path, classes=classes, width=width, height=height, title=title, alt=alt, link=link) whitespace_remover = re.compile(r"^\s+", re.MULTILINE) rendered_content = whitespace_remover.sub("", rendered_content) return rendered_content
def __init__(self, source): self.source = path(source).abspath() """The absolute path to the source file for the post.""" self.html_template_path = 'theme/post_detail.html' """The path to the template to use to transform the post into HTML.""" self.markdown_template_path = 'core/post.md' """The path to the template to use to transform the post back into a :ref:`post source file <posts>`.""" # This will get set to `True in _parse_source if the source file has 'fenced metadata' (like Jekyll) self._fence = False metadata, self._content_raw = self._parse_source() if not hasattr(self, 'content_preprocessed'): self.content_preprocessed = self.content_raw # Handle any preprocessor plugins for plugin in PostProcessor.plugins: plugin.preprocess(self, metadata) self.title = metadata.pop( 'title', self.source.namebase.replace('-', ' ').replace('_', ' ').title()) """The title of the post.""" self.slug = metadata.pop('slug', slugify(self.title)) """The slug for the post.""" self.tags = wrap_list(metadata.pop('tags', [])) """A list of strings representing the tags applied to the post.""" self.link = metadata.pop('link', None) """The post's :ref:`external link <post link>`.""" self.via = metadata.pop('via', None) """The post's attribution name.""" self.via_link = metadata.pop('via-link', metadata.pop('via_link', None)) """The post's attribution link.""" try: self.status = Status(metadata.pop('status', Status.draft.name)) """The status of the post (published or draft).""" except ValueError: logger.warning( "'%s': Invalid status value in metadata. Defaulting to 'draft'." % self.title) self.status = Status.draft self.timestamp = metadata.pop('timestamp', None) """The date/time the post was published or written.""" if self.timestamp is None: self.timestamp = times.now() utctime = True else: utctime = False if not isinstance(self.timestamp, datetime): # looks like the timestamp from YAML wasn't directly convertible to a datetime, so we need to parse it self.timestamp = parser.parse(str(self.timestamp)) if self.timestamp.tzinfo is not None: # parsed timestamp has an associated timezone, so convert it to UTC self.timestamp = times.to_universal(self.timestamp) elif not utctime: # convert to UTC assuming input time is in the DEFAULT_TIMEZONE self.timestamp = times.to_universal(self.timestamp, settings.POST_TIMEZONE) self.content = Post.convert_to_html(self.content_preprocessed) """The post's content in HTML format.""" # determine the URL based on the HOME_URL and the PERMALINK_STYLE settings permalink = settings.PERMALINK_STYLE.format( year=unicode(self.timestamp_local.year), month=u'{0:02d}'.format(self.timestamp_local.month), day=u'{0:02d}'.format(self.timestamp_local.day), i_month=self.timestamp_local.month, i_day=self.timestamp_local.day, title=self.slug, # for Jekyll compatibility slug=self.slug, timestamp=self.timestamp_local, post=self) if permalink.endswith('index.html'): permalink = permalink[:-10] elif permalink.endswith('.html') or permalink.endswith('/'): pass else: permalink += '.html' self._permalink = permalink # keep track of any remaining properties in the post metadata metadata.pop( 'url', None) # remove the url property from the metadata dict before copy self.custom_properties = copy(metadata) """A dict of any custom metadata properties specified in the post.""" # handle any postprocessor plugins for plugin in PostProcessor.plugins: plugin.postprocess(self) # update cache settings.POST_CACHE[self.source] = self
def __init__(self, source): self.source = path(source).abspath() """The absolute path to the source file for the post.""" self.html_template_path = 'theme/post_detail.html' """The path to the template to use to transform the post into HTML.""" self.markdown_template_path = 'core/post.md' """The path to the template to use to transform the post back into a :ref:`post source file <posts>`.""" # This will get set to `True in _parse_source if the source file has 'fenced metadata' (like Jekyll) self._fence = False metadata, self._content_raw = self._parse_source() if not hasattr(self, 'content_preprocessed'): self.content_preprocessed = self.content_raw # Handle any preprocessor plugins for plugin in PostProcessor.plugins: plugin.preprocess(self, metadata) self.title = metadata.pop('title', self.source.namebase.replace('-', ' ').replace('_', ' ').title()) """The title of the post.""" self.slug = metadata.pop('slug', slugify(self.title)) """The slug for the post.""" self.tags = wrap_list(metadata.pop('tags', [])) """A list of strings representing the tags applied to the post.""" self.link = metadata.pop('link', None) """The post's :ref:`external link <post link>`.""" self.via = metadata.pop('via', None) """The post's attribution name.""" self.via_link = metadata.pop('via-link', metadata.pop('via_link', None)) """The post's attribution link.""" try: self.status = Status(metadata.pop('status', Status.draft.name)) """The status of the post (published or draft).""" except ValueError: logger.warning("'%s': Invalid status value in metadata. Defaulting to 'draft'." % self.title) self.status = Status.draft self.timestamp = metadata.pop('timestamp', None) """The date/time the post was published or written.""" if self.timestamp is None: self.timestamp = times.now() utctime = True else: utctime = False if not isinstance(self.timestamp, datetime): # looks like the timestamp from YAML wasn't directly convertible to a datetime, so we need to parse it self.timestamp = parser.parse(str(self.timestamp)) if self.timestamp.tzinfo is not None: # parsed timestamp has an associated timezone, so convert it to UTC self.timestamp = times.to_universal(self.timestamp) elif not utctime: # convert to UTC assuming input time is in the DEFAULT_TIMEZONE self.timestamp = times.to_universal(self.timestamp, settings.POST_TIMEZONE) self.content = Post.convert_to_html(self.content_preprocessed) """The post's content in HTML format.""" # determine the URL based on the HOME_URL and the PERMALINK_STYLE settings permalink = settings.PERMALINK_STYLE.format(year=unicode(self.timestamp_local.year), month=u'{0:02d}'.format(self.timestamp_local.month), day=u'{0:02d}'.format(self.timestamp_local.day), i_month=self.timestamp_local.month, i_day=self.timestamp_local.day, title=self.slug, # for Jekyll compatibility slug=self.slug, timestamp=self.timestamp_local, post=self) if permalink.endswith('index.html'): permalink = permalink[:-10] elif permalink.endswith('.html') or permalink.endswith('/'): pass else: permalink += '.html' self._permalink = permalink # keep track of any remaining properties in the post metadata metadata.pop('url', None) # remove the url property from the metadata dict before copy self.custom_properties = copy(metadata) """A dict of any custom metadata properties specified in the post.""" # handle any postprocessor plugins for plugin in PostProcessor.plugins: plugin.postprocess(self) # update cache settings.POST_CACHE[self.source] = self
def normalize_list(self, p): l = wrap_list(p) return_list = [self.normalize(p) for p in l] return return_list
def _initialize(self, config): self._check_deprecated_settings(config) self.ENGINEER = EngineerConfiguration._EngineerConstants() # CONTENT DIRECTORIES self.SETTINGS_DIR = path(config.pop('SETTINGS_DIR', self.SETTINGS_FILE.dirname().abspath() if self.SETTINGS_FILE is not None else path.getcwd())) self.CONTENT_DIR = self.normalize(config.pop('CONTENT_DIR', 'content')) self.POST_DIR = self.normalize_list(config.pop('POST_DIR', 'posts')) self.OUTPUT_DIR = self.normalize(config.pop('OUTPUT_DIR', 'output')) self.OUTPUT_DIR_IGNORE = wrap_list(config.pop('OUTPUT_DIR_IGNORE', ['.git', '.gitignore'])) self.TEMPLATE_DIR = self.normalize(config.pop('TEMPLATE_DIR', 'templates')) self.TEMPLATE_PAGE_DIR = self.normalize( config.pop('TEMPLATE_PAGE_DIR', (self.TEMPLATE_DIR / 'pages').abspath()) ) self.LOG_DIR = self.normalize(config.pop('LOG_DIR', 'logs')) if self.SETTINGS_FILE is None: self.LOG_FILE = self.normalize(config.pop('LOG_FILE', (self.LOG_DIR / 'build.log').abspath())) else: self.LOG_FILE = self.normalize( config.pop( 'LOG_FILE', (self.LOG_DIR / ('%s-%s.log' % (datetime.now().strftime('%m.%d_%H.%M.%S'), self.SETTINGS_FILE.name))).abspath()) ) self.CACHE_DIR = self.normalize(config.pop('CACHE_DIR', None)) if self.CACHE_DIR is None: if self.SETTINGS_FILE is not None: self.CACHE_DIR = self.normalize('_cache/%s' % self.SETTINGS_FILE.name) else: self.CACHE_DIR = self.normalize('_cache/None') else: self.CACHE_DIR = self.normalize(self.CACHE_DIR) self.CACHE_FILE = self.normalize( config.pop('CACHE_FILE', (self.CACHE_DIR / 'engineer.cache').abspath()) ) self.OUTPUT_CACHE_DIR = self.normalize( config.pop('OUTPUT_CACHE_DIR', (self.CACHE_DIR / 'output_cache').abspath()) ) self.JINJA_CACHE_DIR = self.normalize( config.pop('JINJA_CACHE_DIR', (self.CACHE_DIR / 'jinja_cache').abspath()) ) self.BUILD_STATS_FILE = self.normalize( config.pop('BUILD_STATS_FILE', (self.CACHE_DIR / 'build_stats.cache').abspath()) ) # PLUGINS self.PLUGINS = self.normalize_list(config.pop('PLUGINS', None)) if self.PLUGINS is not None: for plugin in self.PLUGINS: __import__(plugin) # THEMES self.THEME_DIRS = self.normalize_list(config.pop('THEME_DIRS', None)) self.THEME_FINDERS = [ 'engineer.finders.ThemeDirsFinder', 'engineer.finders.SiteFinder', 'engineer.finders.PluginFinder', 'engineer.finders.DefaultFinder' ] self.THEME_SETTINGS = config.pop('THEME_SETTINGS', {}) self.THEME = config.pop('THEME', 'dark_rainbow') # PREPROCESSOR / COMPRESSOR SETTINGS self.COMPRESSOR_ENABLED = config.pop('COMPRESSOR_ENABLED', True) self.COMPRESSOR_FILE_EXTENSIONS = config.pop('COMPRESSOR_FILE_EXTENSIONS', ['js', 'css']) self.PREPROCESS_LESS = config.pop('PREPROCESS_LESS', True) if not 'LESS_PREPROCESSOR' in config: if platform.system() == 'Windows': self.LESS_PREPROCESSOR = str(self.ENGINEER.ROOT_DIR / 'lib/less.js-windows/lessc.cmd') + ' {infile} {outfile}' else: self.LESS_PREPROCESSOR = 'lessc {infile} {outfile}' else: self.LESS_PREPROCESSOR = path(config.pop('LESS_PREPROCESSOR')) # SITE SETTINGS self.SITE_TITLE = config.pop('SITE_TITLE', 'SITE_TITLE') self.SITE_URL = config.pop('SITE_URL', 'SITE_URL') self.SITE_AUTHOR = config.pop('SITE_AUTHOR', None) self.HOME_URL = config.pop('HOME_URL', '/') # HOME_URL must end with a slash if not self.HOME_URL.endswith('/'): self.HOME_URL += '/' self.STATIC_URL = config.pop('STATIC_URL', urljoin(self.HOME_URL, 'static')) # starting in version 0.5, the default permalink style will change to 'pretty' permalink_setting = config.pop('PERMALINK_STYLE', None) if permalink_setting is None: self.PERMALINK_STYLE = permalink_styles['pretty'] else: self.PERMALINK_STYLE = permalink_styles.get(permalink_setting, permalink_setting) self.ROLLUP_PAGE_SIZE = int(config.pop('ROLLUP_PAGE_SIZE', 5)) # RSS FEED SETTINGS self.FEED_TITLE = config.pop('FEED_TITLE', self.SITE_TITLE + ' Feed') self.FEED_ITEM_LIMIT = config.pop('FEED_ITEM_LIMIT', self.ROLLUP_PAGE_SIZE) self.FEED_DESCRIPTION = config.pop('FEED_DESCRIPTION', 'The %s most recent posts from %s.' % (self.FEED_ITEM_LIMIT, self.SITE_URL)) self.FEED_URL = config.pop('FEED_URL', urljoin(self.HOME_URL, 'feeds/atom.xml')) # These 'constants' are updated here so they're relative to the STATIC_URL value lib_path = urljoin(self.STATIC_URL, 'engineer/lib') self.ENGINEER.FOUNDATION_CSS_URL = urljoin(lib_path, 'foundation/') self.ENGINEER.JQUERY_URL = urljoin(lib_path, self.ENGINEER.JQUERY) self.ENGINEER.LESS_JS_URL = urljoin(lib_path, self.ENGINEER.LESS_JS) self.ENGINEER.MODERNIZR_URL = urljoin(lib_path, self.ENGINEER.MODERNIZR) self.ENGINEER.NORMALIZE_CSS_URL = urljoin(lib_path, self.ENGINEER.NORMALIZE_CSS) # URL helper functions def page(num): page_path = urljoin('page', str(num)) return urljoin(self.HOME_URL, page_path) def tag(name): page_path = urljoin('tag', slugify(name)) page_path = urljoin(self.HOME_URL, page_path) return page_path self.URLS = { 'home': self.HOME_URL, 'archives': urljoin(self.HOME_URL, 'archives'), 'feed': self.FEED_URL, 'listpage': page, 'tag': tag, } # Update URLs from the config setting if they're present self.URLS.update(config.pop('URLS', {})) # MISCELLANEOUS SETTINGS self.ACTIVE_NAV_CLASS = config.pop('ACTIVE_NAV_CLASS', 'current') self.DEBUG = config.pop('DEBUG', False) #self.DISABLE_CACHE = config.pop('DISABLE_CACHE', False) self.PLUGIN_PERMISSIONS = { 'MODIFY_RAW_POST': [] } provided_permissions = config.pop('PLUGIN_PERMISSIONS', {}) update_additive(self.PLUGIN_PERMISSIONS, provided_permissions) self.PUBLISH_DRAFTS = config.pop('PUBLISH_DRAFTS', False) self.PUBLISH_PENDING = config.pop('PUBLISH_PENDING', False) self.PUBLISH_REVIEW = config.pop('PUBLISH_REVIEW', False) self.POST_TIMEZONE = pytz.timezone(config.pop('POST_TIMEZONE', 'UTC')) self.SERVER_TIMEZONE = self.POST_TIMEZONE if config.get('SERVER_TIMEZONE', None) is None else config.pop('SERVER_TIMEZONE') self.TIME_FORMAT = config.pop('TIME_FORMAT', '%I:%M %p %A, %B %d, %Y %Z') # '%Y-%m-%d %H:%M:%S %Z%z' # Let plugins deal with their settings in their own way if needed for plugin_type in get_all_plugin_types(): for plugin in plugin_type.plugins: logger.debug("Calling handle_settings on plugin: %s. config dict is: %s" % (plugin, config)) config = plugin.handle_settings(config, self) # Pull any remaining settings in the config and set them as attributes on the settings object for k, v in config.iteritems(): setattr(self, k, v)
def normalize_list(self, p): l = wrap_list(p) return_list = [] for p in l: return_list.append(self.normalize(p)) return return_list