def on_after_build_all(self, builder, **extra): """Once the build process is over : - write the translation template `contents.pot` on the filesystem, - write all translation contents+<language>.po files """ if not self.enabled: return contents_pot_filename = join(builder.env.root_path, self.i18npath, 'contents.pot') pots = [ contents_pot_filename, self.get_templates_pot_filename(), join(builder.env.root_path, self.i18npath, 'plugins.pot') ] # write out contents.pot from web site contents translations.write_pot(pots[0], self.content_language) reporter.report_generic("%s generated" % relpath(pots[0], builder.env.root_path)) pots = [p for p in pots if os.path.exists(p)] # only keep existing ones if len(pots) > 1: translations.merge_pot(pots, contents_pot_filename) reporter.report_generic( "Merged POT files %s" % ', '.join(relpath(p, builder.env.root_path) for p in pots)) for language in self.translations_languages: po_file = POFile(language, self.i18npath) po_file.generate()
def build_artifact(self, artifact): ctx = get_ctx() feed_source = self.source project_id = ctx.env.project.id feed = make_feed(self.source, project_id) for item in get_items(ctx, feed_source): try: _id = get_id('{0}/{1}'.format( project_id, item['_path'].encode('utf-8'))), with ctx.changed_base_url(item.url_path): feed_item = FeedItem(item, feed_source) feed.add( feed_item.title, feed_item.body, xml_base=feed_item.url, url=feed_item.url, content_type='html', id=_id, author=feed_item.author, updated=feed_item.updated, ) except Exception as exc: reporter.report_generic(exc) with artifact.open('wb') as f: f.write(feed.to_string().encode('utf-8'))
def on_before_build_all(self, builder, **extra): if self.enabled: reporter.report_generic("i18n activated, with main language %s"% self.content_language ) templates_pot_filename = join(tempfile.gettempdir(), 'templates.pot') reporter.report_generic("Parsing templates for i18n into %s" \ % relpath(templates_pot_filename, builder.env.root_path)) translations.parse_templates(templates_pot_filename)
def on_setup_env(self, **extra): """Setup `env` for the plugin""" # Read configuration self.enabled = self.get_config().get('enable', 'true') in ('true','True','1') if not self.enabled: reporter.report_generic('I18N plugin disabled in configs/i18n.ini') self.i18npath = self.get_config().get('i18npath', 'i18n') self.url_prefix = self.get_config().get('url_prefix', 'http://localhost/') # whether or not to use a pargraph as smallest translatable unit self.trans_parwise = self.get_config().get('translate_paragraphwise', 'false') in ('true','True','1') self.content_language=self.get_config().get('content', 'en') self.env.jinja_env.add_extension('jinja2.ext.i18n') self.env.jinja_env.policies['ext.i18n.trimmed'] = True # do a .strip() self.env.jinja_env.install_gettext_translations(TemplateTranslator(self.i18npath)) # ToDo: is this still required try: self.translations_languages=self.get_config().get('translations').replace(' ','').split(',') except AttributeError: raise RuntimeError('Please specify the "translations" configuration option in configs/i18n.ini') if not self.content_language in self.translations_languages: self.translations_languages.append(self.content_language) self.env.jinja_env.filters['translate'] = self.translate_tag self.env.jinja_env.globals['_'] = self.translate_tag self.env.jinja_env.globals['choose_language'] = self.choose_language
def on_before_build_all(self, builder, **extra): if self.enabled: reporter.report_generic("i18n activated, with main language %s"% self.content_language ) templates_pot_filename = self.get_templates_pot_filename() reporter.report_generic("Parsing templates for i18n into %s" \ % relpath(templates_pot_filename, builder.env.root_path)) translations.parse_templates(templates_pot_filename)
def on_after_build_all(self, builder): """Once the build process is over : - write the translation template `contents.pot` on the filesystem, - write all translation contents+<language>.po files """ if self.enabled and self.flag_is_present(builder.extra_flags): contents_pot_filename = join(builder.env.root_path, self.i18npath, 'contents.pot') templates_pot_filename = join(tempfile.gettempdir(), 'templates.pot') translations.write_pot(contents_pot_filename, self.content_language) reporter.report_generic( "%s generated" % relpath(contents_pot_filename, builder.env.root_path)) if exists(templates_pot_filename): translations.merge_pot( [contents_pot_filename, templates_pot_filename], contents_pot_filename) reporter.report_generic( "%s merged into %s" % (relpath(templates_pot_filename, builder.env.root_path), relpath(contents_pot_filename, builder.env.root_path))) for language in self.translations_languages: po_file = POFile(language, self.i18npath) po_file.generate()
def on_setup_env(self, **extra): """Setup `env` for the plugin""" # Read configuration self.enabled = self.get_config().get('enable', 'true') in ('true', 'True', '1') if not self.enabled: reporter.report_generic('I18N plugin disabled in configs/i18n.ini') return self.i18npath = self.get_config().get('i18npath', 'i18n') self.url_prefix = self.get_config().get('url_prefix', 'http://localhost/') self.content_language = self.get_config().get('content', 'en') try: self.translations_languages = self.get_config().get( 'translations').replace(' ', '').split(',') except AttributeError: raise RuntimeError( 'Please specify the "translations" configuration option in configs/i18n.ini' ) if not self.content_language in self.translations_languages: self.translations_languages.append(self.content_language) self.env.jinja_env.filters['translate'] = self.translate_tag self.env.jinja_env.globals['_'] = self.translate_tag self.env.jinja_env.globals['choose_language'] = self.choose_language
def on_server_spawn(self, **extra): extra_flags = extra.get("extra_flags") or extra.get("build_flags") or {} if not self.is_enabled(extra_flags): return self.npm_install() reporter.report_generic('Spawning webpack watcher') self.webpack_process = self.run_webpack(watch=True)
def parse_flags(self, builder): """Parse the flags of the provided builder""" try: # Lektor 3+ flags = builder.extra_flags except AttributeError: # Lektor 2 flags = builder.build_flags types = set() allowed_kinds = set(MATCHERS.keys()) if MINIFY_FLAG in flags: if flags[MINIFY_FLAG] == MINIFY_FLAG: types = allowed_kinds else: kinds = set(flags[MINIFY_FLAG].split(",")) diff = kinds - allowed_kinds for kind in diff: reporter.report_generic( "\033[33mUnknown param for flag %s:\033[37m %s" % (MINIFY_FLAG, kind) ) types = kinds & allowed_kinds builder.__can_minify = types return types
def on_before_build_all(self, builder, **extra): if not self.is_enabled(builder.build_flags) \ or self.webpack_process is not None: return self.npm_install() reporter.report_generic('Starting webpack build') self.run_webpack().wait() reporter.report_generic('Webpack build finished')
def on_before_build_all(self, builder, **extra): if self.proc_manager or not self.is_enabled(builder.extra_flags): return reporter.report_generic('Starting npm build') for r in self.runners(): r.build(self.proc_manager) self.proc_manager.wait() reporter.report_generic('Finished npm build')
def on_server_spawn(self, **extra): extra_flags = extra.get("extra_flags") or extra.get( "build_flags") or {} if not self.is_enabled(extra_flags): return self.install_node_dependencies() reporter.report_generic('Spawning {} watcher'.format(self.get_name())) self.webpack_process = self.spawn_watch()
def on_before_build_all(self, builder, **extra): extra_flags = getattr(builder, "extra_flags", getattr(builder, "build_flags", None)) if not self.is_enabled(extra_flags) or self.gulp_process is not None: return self.npm_install() reporter.report_generic('Starting gulp build') self.run_gulp().wait() reporter.report_generic('Gulp build finished')
def _fs_mtime(filename): try: st = os.stat(filename) except OSError as exc: reporter.report_generic("{}: {!s}".format(filename, exc)) return None else: # (truncate to one second resolution) return int(st.st_mtime)
def on_after_build_all(self, builder, **extra): pot_filename = join(builder.env.root_path, self.i18npath, 'contents.pot') with open(pot_filename,'w') as f: f.write(translations.as_pot(self.content_language).encode("utf-8")) reporter.report_generic("%s generated"%pot_filename) for language in self.translations_languages: po_file=POFile(language, self.i18npath) po_file.generate()
def on_before_build_all(self, builder, **extra): extra_flags = getattr(builder, "extra_flags", getattr(builder, "build_flags", None)) if not self.is_enabled(extra_flags) \ or self.webpack_process is not None: return self.install_node_dependencies() reporter.report_generic('Starting webpack build') self.run_webpack().wait() reporter.report_generic('Webpack build finished')
def on_before_build_all(self, builder): if self.enabled and self.flag_is_present(builder.extra_flags): reporter.report_generic("i18n activated, with main language %s" % self.content_language) templates_pot_filename = join(tempfile.gettempdir(), 'templates.pot') reporter.report_generic( "Parsing templates for i18n into %s" % relpath(templates_pot_filename, builder.env.root_path)) translations.parse_templates(templates_pot_filename)
def run_package_manager(self, *args): # Use yarn over npm if it's availabe and there is a yarn lockfile has_yarn_lockfile = os.path.exists( self.get_webpack_folder('yarn.lock')) pkg_manager = 'npm' if locate_executable('yarn') is not None and has_yarn_lockfile: pkg_manager = 'yarn' reporter.report_generic('Running {} {}'.format(pkg_manager, " ".join(args))) return portable_popen([pkg_manager] + list(args), cwd=self.get_webpack_folder())
def install_node_dependencies(self): webpack_root = os.path.join(self.env.root_path, 'webpack') # Use yarn over npm if it's availabe and there is a yarn lockfile has_yarn_lockfile = os.path.exists( os.path.join(webpack_root, 'yarn.lock')) pkg_manager = 'npm' if locate_executable('yarn') is not None and has_yarn_lockfile: pkg_manager = 'yarn' reporter.report_generic('Running {} install'.format(pkg_manager)) portable_popen([pkg_manager, 'install'], cwd=webpack_root).wait()
def on_before_build_all(self, builder, **extra): if not self.is_enabled(builder): return if 'uncss' in self.options.sections(): self.npm_install() if "sass" in self.options.sections(): reporter.report_generic("Starting sass") source = self.get_option_path("sass.source", builder) output = self.get_option_path("sass.output", builder) self.run_sass(source, output) reporter.report_generic("Finished sass")
def on_after_build_all(self, builder, **extra): if not self.is_enabled(builder): return if "uncss" in self.options.sections(): reporter.report_generic("Starting uncss") asset_dirs = tuple( os.path.join(builder.destination_path, d) for d in os.listdir(self.env.asset_path) ) sources = [ f for f in find_files(builder.destination_path, "*.html") if not f.startswith(asset_dirs) ] output = self.get_option_path("uncss.output", builder) self.run_uncss(sources, output) reporter.report_generic("Finished uncss") if "cssmin" in self.options.sections(): reporter.report_generic("Starting cssmin") source = self.get_option_path("cssmin.source", builder) output = self.get_option_path("cssmin.output", builder) self.run_cssmin(source, output) reporter.report_generic("Finished cssmin")
def on_after_build_all(self, builder, **extra): """ Once the build process is over, save `COPY_FILES` to assets. """ for f in self.COPY_FILES: fpath = os.path.join(builder.env.root_path, f) fbuildpath = os.path.join(builder.destination_path, f) if os.path.isfile(fbuildpath): os.remove(fbuildpath) if os.path.isfile(fpath): shutil.copy(fpath, fbuildpath) reporter.report_generic( "{} copied to output assets folder".format(f))
def on_setup_env(self, **extra): prefs = self.get_config() endswith = prefs.get('endswith') if endswith is None: raise RuntimeError(self.msg_init_config) self.enabled = asBool(prefs.get('enabled'), default=True) if self.enabled: for pattern in endswith.split(','): self.patterns.append(pattern.strip()) if len(self.patterns) == 0: self.enabled = False readable = 'ENABLED' if self.enabled else 'DISABLED' reporter.report_generic('Plugin ' + readable + ': force-update')
def on_after_build_all(self, builder, **extra): """ after-build-all lektor event """ # NOTE(vesuvium): compatibility for lektor 2.X and 3.X try: is_enabled = self.is_enabled(builder.build_flags) except AttributeError: is_enabled = self.is_enabled(builder.extra_flags) if not is_enabled: return reporter.report_generic('HTML minification started') for htmlfile in self.find_html_files(builder.destination_path): self.minify_file(htmlfile) reporter.report_generic('HTML minification finished')
def on_after_build_all(self, builder, **extra): """ after-build-all lektor event """ # NOTE(vesuvium): compatibility for lektor 2.X and 3.X try: is_enabled = self.is_enabled(builder.build_flags) except AttributeError: is_enabled = self.is_enabled(builder.extra_flags) if not is_enabled: return reporter.report_generic('Starting HTML minification') for htmlfile in self.find_html_files(builder.destination_path): self.minify_file(htmlfile) reporter.report_generic('HTML minification finished')
def on_after_build_all(self, builder, **extra): """Dump each alternative entry of the self.staic_search dict to json and save it.""" for alt, pages in self.static_search.items(): filename = os.path.join(builder.env.root_path, self.options['output_path'], 'static_search_{}.json'.format(alt)) try: with open(filename, 'r') as f: contents = f.read() except IOError: contents = "" if contents != json.dumps(pages): with open(filename, 'w') as f: f.write(json.dumps(pages)) reporter.report_generic( 'generated static search file{}'.format(filename))
def get_default_child_slug(self, pad, data): """Formats out the child slug.""" slug_format = self.child_config.slug_format if slug_format is None: return data["_id"] if self._child_slug_tmpl is None or self._child_slug_tmpl[ 0] != slug_format: self._child_slug_tmpl = ( slug_format, FormatExpression(self.env, slug_format), ) try: return "_".join(self._child_slug_tmpl[1].evaluate( pad, this=data).strip().split()).strip("/") except Exception as exc: reporter.report_generic("Failed to expand child slug_format: %s" % exc) return "temp-" + slugify(data["_id"])
def on_after_build_all(self, builder, **extra): """Once the build process is over : - write the translation template `contents.pot` on the filesystem, - write all translation contents+<language>.po files """ if not self.enabled: return contents_pot_filename = join(builder.env.root_path, self.i18npath, 'contents.pot') pots = [contents_pot_filename, join(tempfile.gettempdir(), 'templates.pot'), join(builder.env.root_path, self.i18npath, 'plugins.pot')] # write out contents.pot from web site contents translations.write_pot(pots[0], self.content_language) reporter.report_generic("%s generated" % relpath(pots[0], builder.env.root_path)) pots = [p for p in pots if os.path.exists(p) ] # only keep existing ones if len(pots) > 1: translations.merge_pot(pots, contents_pot_filename) reporter.report_generic("Merged POT files %s" % ', '.join( relpath(p, builder.env.root_path) for p in pots)) for language in self.translations_languages: po_file=POFile(language, self.i18npath) po_file.generate()
def on_setup_env(self): """Setup `env` for the plugin""" # Read configuration self.enabled = self.get_config().get('enable', 'true') in ('true','True','1') if not self.enabled: reporter.report_generic('I18N plugin disabled in configs/i18n.ini') self.i18npath = self.get_config().get('i18npath', 'i18n') self.url_prefix = self.get_config().get('url_prefix', 'http://localhost/') # whether or not to use a pargraph as smallest translatable unit self.trans_parwise = self.get_config().get('translate_paragraphwise', 'false') in ('true','True','1') self.content_language=self.get_config().get('content', 'en') self.env.jinja_env.add_extension('jinja2.ext.i18n') self.env.jinja_env.policies['ext.i18n.trimmed'] = True # do a .strip() self.env.jinja_env.install_gettext_translations(TemplateTranslator(self.i18npath)) # ToDo: is this stil required try: self.translations_languages=self.get_config().get('translations').replace(' ','').split(',') except AttributeError: raise RuntimeError('Please specify the "translations" configuration option in configs/i18n.ini') if not self.content_language in self.translations_languages: self.translations_languages.append(self.content_language)
def on_server_spawn(self, build_flags, **extra): self.npm_install() reporter.report_generic('Spawning Brunch watcher') self.brunch_process = self.run_brunch(build_flags, watch=True)
def npm_install(self): reporter.report_generic('Installing NPM dependencies') brunch_root = os.path.join(self.env.root_path, 'brunch') portable_popen(['npm', 'install'], cwd=brunch_root).wait()
def on_server_stop(self, **extra): if self.webpack_process is not None: reporter.report_generic('Stopping webpack watcher') self.webpack_process.kill()
def npm_install(self): reporter.report_generic('Running npm install') webpack_root = os.path.join(self.env.root_path, 'webpack') portable_popen(['npm', 'install'], cwd=webpack_root).wait()
def npm_install(self): reporter.report_generic("Running npm install") npm_root = os.path.join(self.env.root_path, ".") portable_popen(["npm", "install"], cwd=npm_root).wait()
def on_server_spawn(self, build_flags, **extra): if not self.is_enabled(build_flags): return self.npm_install() reporter.report_generic('Spawning webpack watcher') self.webpack_process = self.run_webpack(watch=True)
def on_server_stop(self, **extra): if self.brunch_process is not None: reporter.report_generic('Stopping Brunch watcher') self.brunch_process.kill()
def on_before_build_all(self, builder, **extra): self.npm_install() reporter.report_generic('Starting Brunch build') self.run_brunch(builder.build_flags).wait() reporter.report_generic('Brunch build finished')