Beispiel #1
0
    def generate_po_file(self):
        for language in config.get_languages():
            locale_path = os.path.join(config.locales_path, language,
                                       'LC_MESSAGES')
            po_file_path = os.path.join(locale_path, self.po_filename)

            po_content = ''
            if not os.path.isdir(locale_path):
                os.makedirs(locale_path)

            if not os.path.exists(po_file_path):
                with open(self.pot_file_path, 'r') as f:
                    po_content = f.read()
            else:
                msgmerge_args = ['msgmerge'] + self.msgmerge_options + [
                    po_file_path, self.pot_file_path
                ]
                po_content, errors, status = call_subprocess(msgmerge_args)
                if status != constants.STATUS_OK and errors:
                    logger.error(
                        "errors happened while running msgmerge\n{}".format(
                            errors))
                    sys.exit(1)
                elif errors:
                    logger.error(errors)

            with open(po_file_path, 'w', encoding='utf-8') as f:
                f.write(po_content)

            logger.info(
                '«.po» file generated for language «{}» at «{}»'.format(
                    language, po_file_path))
Beispiel #2
0
    def __init__(self):

        languages = config.get_languages()
        if len(languages) == 1 and languages[0] == constants.DEFAULT_LANG:
            logger.error(
                'No languages configuration detected, can\'t create messages')
            sys.exit(1)

        self.po_filename = '{}.po'.format(constants.GETTEXT_DOMAIN)
        self.mo_filename = '{}.mo'.format(constants.GETTEXT_DOMAIN)
Beispiel #3
0
    def _get_content(self, content_path):
        frontmatter, content = page_parser.load(content_path)

        relative_path = content_path.replace(config.content_path,
                                             "").strip(os.sep)
        path_chunks = relative_path.split(os.sep)
        filename = path_chunks[-1]

        language = config.main_language
        languages = config.get_languages()
        filename_parts = filename.split('.')
        name = "_".join(filename_parts[0:-1])
        extension = filename_parts[-1]
        if len(filename_parts) > 2:
            posible_lang = filename_parts[-2]
            language = posible_lang if posible_lang in languages else language
            name = "_".join(filename_parts[0:-2])

        page_path = path_chunks[0:-1]
        section = None
        page = Page(frontmatter, content, name, extension, relative_path,
                    page_path, language)
        page.set_modification_date(os.path.getmtime(content_path))
        page_is_section = False
        page_is_homepage = False

        if len(path_chunks) > 1:
            section_name = path_chunks[0]
        elif page.name == 'index':
            page_is_homepage = True
        else:
            page_is_section = True
            section_name = page.name

        # Index content means section content
        if len(path_chunks) == 2 and page.name == 'index':
            page_is_section = True

        page_to_return = page
        if page_is_homepage:
            page_to_return = None
            self.homepages[language].set_content_page(page)
        elif page.to_publish():
            section = self._get_section(section_name, language)
            if page_is_section:
                section.set_content_page(page)
                page_to_return = None
            else:
                section.add_page(page)

            self.update_taxonomies(page)

        return page_to_return
Beispiel #4
0
    def __init__(self):

        languages = config.get_languages()
        if len(languages) == 1 and languages[0] == constants.DEFAULT_LANG:
            logger.error(
                'No languages configuration detected, can\'t create messages')
            sys.exit(1)

        loader = FileSystemLoader(config.files_path)

        self.env = Environment(extensions=['jinja2.ext.i18n'], loader=loader)
        self.env.filters['clean_msg'] = clean_msg

        self.translations = []
        self.pot_filename = '{}.pot'.format(constants.GETTEXT_DOMAIN)
        self.po_filename = '{}.po'.format(constants.GETTEXT_DOMAIN)
        self.pot_file_path = os.path.join(config.locales_path,
                                          self.pot_filename)
Beispiel #5
0
    def handle_routed_content(self, content):

        languages = config.get_languages()

        if not isinstance(content, dict):
            logger.warning(
                'Every element in the routing file must be a dictionary')
            return

        mandatory_fields = [('title', str), ('section', str), ('url', str)]

        for field, field_type in mandatory_fields:
            value = content.get(field, None)
            if not value:
                logger.warning(
                    'Every routed page must have a «{}» attribute'.format(
                        field))
                return
            if not isinstance(value, field_type):
                logger.warning('«{}» attribute must be type «{}»'.format(
                    field, field_type))
                return

        language = config.main_language
        if 'language' in content:
            language = content['language'] if content[
                'language'] in languages else language
            del content['language']

        section_name = None
        if 'section' in content:
            section_name = content.get('section')
            del content['section']

        routed_page = RoutedPage(content, language)

        if routed_page.to_publish():
            section = self._get_section(section_name, language)
            section.add_page(routed_page)

            self.update_taxonomies(routed_page)

        return routed_page
Beispiel #6
0
    def initialize(self):
        self.contents = defaultdict(list)
        self.expired_contents = defaultdict(list)

        self.taxonomy_definitions = defaultdict(dict)
        self.taxonomies = defaultdict(dict)
        self.sections = defaultdict(dict)
        self.rss = defaultdict(list)
        self.search_pages = {}

        self.homepages = {}
        for lang in config.get_languages():
            self.homepages[lang] = homepage = Homepage(lang)
            self.rss[lang].append(Rss(lang, homepage))
            self.search_pages[lang] = SearchPage(lang)

            self.taxonomy_definitions[lang] = {
                singular: TaxonomyDefinition(singular, plural)
                for singular, plural in config.get_taxonomies(lang).items()
            }
Beispiel #7
0
    def compile(self):
        for language in config.get_languages():
            locale_path = os.path.join(config.locales_path, language,
                                       'LC_MESSAGES')
            po_file_path = os.path.join(locale_path, self.po_filename)
            mo_file_path = os.path.join(locale_path, self.mo_filename)

            if has_bom(po_file_path):
                logger.error("The {} file has a BOM (Byte Order Mark). "
                             "Sitic only supports .po files encoded in "
                             "UTF-8 and without any BOM.".format(po_file_path))

            if not os.path.exists(po_file_path):
                logger.warning(
                    "Not .po file found for language «{}»."
                    "Run «makemessages» to generate it.".format(language))
                continue

            msgmt_args = ['msgfmt'] + self.msgfmt_options + [
                '-o', mo_file_path, po_file_path
            ]
            output, errors, status = call_subprocess(msgmt_args)

            if status != constants.STATUS_OK and errors:
                logger.error(
                    "errors happened while running msgmerge\n{}".format(
                        errors))
                sys.exit(1)
            elif errors:
                logger.error(errors)

            logger.info(
                'Messages successfully compiled for language «{}»'.format(
                    language))

        logger.warning(
            'Please, keep in mind that «msgfmt» won\'t generate any «.mo» file if no translation modified'
        )
Beispiel #8
0
    def gen(self):
        logger.info('Generating site...')

        self.current_url = None

        stats.initialize()
        self.build_contents()
        self.create_public_folder()
        self.move_static_folder()

        self.search_indexer = SearchIndexer()

        for language in config.get_languages():

            # initialize context every loop
            self.context['site'] = {
                'disqus_shortname': config.disqus_shortname,
                'language': language,
                'main_language': config.main_language,
                'languages': config.get_languages_config(),
            }

            render = Render(language)
            sitemap = Sitemap(language)

            contents = self.content_factory.get_contents(language)
            expired_contents = self.content_factory.expired_contents[language]

            taxonomies = self.content_factory.get_taxonomies(language)
            taxonomies_contents = self.get_taxonomies_content(taxonomies)
            sections = self.content_factory.get_sections(language)
            search_page = self.content_factory.search_pages[language]

            self.add_taxonomies_to_context(taxonomies)

            homepage = self.content_factory.homepages[language]
            homepage.pages = contents
            self.context['site']['home_url'] = homepage.get_url()

            rss = self.content_factory.rss[language]

            menu_builder = MenuBuilder(self, contents, sections, language)

            menus = menu_builder.build()
            self.context['site']['menus'] = menus

            all_contents = [homepage] + contents + taxonomies_contents \
                    + sections

            if not config.disable_rss:
                all_contents += rss

            if not config.disable_search and len(contents) > 0:
                all_contents += [search_page]

            meta_data = {
                'search-index': self.search_indexer.get_url(),
                'language': language,
            }

            self.search_indexer.add_contents(contents + sections)

            meta_values = [
                'data-{}="{}"'.format(key, value)
                for key, value in meta_data.items() if value
            ]
            self.meta_tag = '<meta name="sitic" {}/>'.format(
                ' '.join(meta_values))

            for content in all_contents:
                self.current_url = content.get_url()
                stats.update(content)
                self.context['scoper'] = Scoper()
                if content.is_paginable():
                    self.generate_paginable(render, content)
                else:
                    self.generate_regular(render, content)

                # FIXME: temporary fix
                if not isinstance(content, Rss):
                    sitemap.contents.append(content)

                if content.has_redirect_url():
                    all_contents.append(
                        RedirectPage(content.get_redirect_url(),
                                     content.get_url()))

            if not config.disable_sitemap:
                self.generate_regular(render, sitemap)

            if not config.remove_expired:
                self.remove_expired(expired_contents)

        if not config.disable_search:
            self.search_indexer.create_files()

        logger.info('{}{}{}'.format('Site generated', os.linesep,
                                    stats.get_stats()))