def writepage (eventjson, p):
    fpath = pagepath(eventjson)
    with open(fpath, mode="w+b") as f:
        o = io.BytesIO()
        frontmatter.dump(p, o, handler=frontmatter.YAMLHandler())
        f.write(o.getvalue())
        print("Wrote page for event " + eventjson["name"] + " to " + fpath)
Beispiel #2
0
    def create_front_matter(self):
        if not self.format or self.format == 'none':
            return ''

        if len(
                self.contents
        ) == 0:  # if there is no meta data do not create an empty header
            return ''

        # if frontmatter.checks(content):
        #     _, content = frontmatter.parse(content)  # remove metadata if pandoc has added it (pandoc v2.13 and above)

        if self.format == 'text':
            return self.generate_plain_text_front_matter()

        front_matter_post = frontmatter.Post('')

        # iterate metadata items rather than using "frontmatter.Post(content, **self._metadata)"
        # because POST init can not accept a meta data field that has a key of 'content' which is common in html
        # and likely in other files as well
        for key, value in self.contents.items():
            front_matter_post[key] = value

        if self.format == 'yaml':
            return frontmatter.dumps(front_matter_post,
                                     handler=frontmatter.YAMLHandler())
        if self.format == 'toml':
            return frontmatter.dumps(front_matter_post,
                                     handler=frontmatter.TOMLHandler())
        if self.format == 'json':
            return frontmatter.dumps(front_matter_post,
                                     handler=frontmatter.JSONHandler())
def generate_wps_options(docs: Path) -> None:
    """Generate configuration defaults for current version of WPS."""
    from wemake_python_styleguide.options import defaults
    docs.mkdir(parents=True, exist_ok=True)

    with open(defaults.__file__, 'r') as f:
        module = libcst.parse_module(f.read())

    for statement in module.body:
        assignment = statement.body[0]

        if not isinstance(assignment, libcst.AnnAssign):
            continue

        name = assignment.target.value

        value = getattr(defaults, name)
        last_leading_line = statement.leading_lines[-1]
        description = last_leading_line.comment.value.lstrip(
            '#: ',
        ).replace('``', '`')

        reasoning = format_reasoning(statement.trailing_whitespace.comment)

        cli_name = '--' + name.lower().replace('_', '-')

        parameter = WPSConfigurationParameter(
            about=f'python://wemake_python_styleguide.options.defaults.{name}',
            name=name,
            cli_name=cli_name,
            value=str(value),
            description=description,
            reasoning=reasoning,
        )

        document = frontmatter.Post(
            content=parameter.description,
            handler=frontmatter.YAMLHandler(),
            **parameter.dict(
                exclude={'description'},
                exclude_defaults=True,
            ),
        )

        output_path = docs / f'{parameter.name}.md'

        with output_path.open('wb+') as output_file:
            frontmatter.dump(
                document,
                output_file,
            )
Beispiel #4
0
def document_violation(
    version_directory: Path,
    violation: BugbearViolation,
) -> None:
    """Write violation description into a Markdown file."""
    document = frontmatter.Post(content=violation.message,
                                handler=frontmatter.YAMLHandler(),
                                **violation.dict(
                                    exclude={'message', 'type', 'vars'},
                                    exclude_none=True,
                                ))

    (version_directory / f'{violation.code}.md').write_text(
        frontmatter.dumps(document), )
Beispiel #5
0
def write_violations_to_disk(
    violations: Iterator[Violation],
    directory: Path,
) -> None:
    for violation in violations:
        violation_path = directory / f'{violation.code}.md'
        document = frontmatter.Post(
            content=violation.description,
            handler=frontmatter.YAMLHandler(),
            **violation.dict(
                exclude={'description', 'output_file'},
                by_alias=True,
                exclude_none=True,
            ),
        )

        with open(violation_path, 'wb+') as code_file:
            frontmatter.dump(document, code_file)
Beispiel #6
0
    def add_metadata_md_to_content(self, content):
        self.logger.debug(f"Add front matter meta-data to markdown page")
        if self._conversion_settings.front_matter_format == 'none':
            return content

        if len(
                self._metadata
        ) == 0:  # if there is no meta data do not create an empty header
            return content

        if frontmatter.checks(content):
            _, content = frontmatter.parse(
                content
            )  # remove metadata if pandoc has added it (pandoc v2.13 and above)

        if self._conversion_settings.front_matter_format == 'text':
            content = self.add_text_metadata_to_content(content)
            return content

        merged_content = frontmatter.Post(content)

        # iterate metadata items rather than using "frontmatter.Post(content, **self._metadata)"
        # because POST init can not accept a meta data field that has a key of 'content' which is common in html
        # and likely in other files as well
        self.format_ctime_and_mtime_if_required()
        self.change_displayed_created_time_if_required()
        self.change_displayed_modified_time_if_required()

        for key, value in self._metadata.items():
            merged_content[key] = value

        self._force_pandoc_markdown_to_yaml_front_matter()

        if self._conversion_settings.front_matter_format == 'yaml':
            content = frontmatter.dumps(merged_content,
                                        handler=frontmatter.YAMLHandler())
        if self._conversion_settings.front_matter_format == 'toml':
            content = frontmatter.dumps(merged_content,
                                        handler=frontmatter.TOMLHandler())
        if self._conversion_settings.front_matter_format == 'json':
            content = frontmatter.dumps(merged_content,
                                        handler=frontmatter.JSONHandler())

        return content
Beispiel #7
0
def create_version_index_md(docs: Path, package_version: str) -> None:
    """Write version description."""
    v = version.parse(package_version)
    post = frontmatter.Post(
        content='',
        handler=frontmatter.YAMLHandler(),
        major_version=v.major,
        minor_version=v.minor,
        patch_version=v.micro,
    )

    post_content = frontmatter.dumps(post)
    text_content = f'{post_content}\n\n'

    try:
        (docs / 'index.md').write_text(text_content)
    except FileNotFoundError:
        docs.mkdir(parents=True, exist_ok=True)
        (docs / 'index.md').write_text(text_content)
Beispiel #8
0
def persist_constant(
    constant: WPSConstant,
    directory: Path,
):
    post = frontmatter.Post(
        content=constant.description,
        handler=frontmatter.YAMLHandler(),

        # To avoid yaml.representer.RepresenterError
        about=str(constant.about),
        **constant.dict(
            exclude={'description', 'about'},
            exclude_none=True,
            by_alias=True,
        ),
    )

    with (directory / f'{constant.name}.md').open('wb+') as output_file:
        frontmatter.dump(
            post=post,
            fd=output_file,
        )
Beispiel #9
0
                a['rel'] = 'nofollow'
                del a['target']

            # View in browser link
            anchors[0].decompose()
            # Unsubscribe link
            anchors[len(anchors) - 2].decompose()
            # Mailerlite link
            anchors[len(anchors) - 1].decompose()

            content += '\n\n\n' + body.prettify()

            post = frontmatter.Post(
                content,
                date=dateutil.parser.parse(email_message["date"]).isoformat(),
                title=str(d.title.string),
                slug=str(d.title.string),
                categories=['Newsletter'],
                tags=[],
            )

            o = io.BytesIO()
            frontmatter.dump(post, o, handler=frontmatter.YAMLHandler())
            with open(fpath, 'w+b') as f:
                f.write(o.getvalue())
                print('Newsletter \"' + str(d.title.string) +
                      '\" written to ' + fpath)

        else:
            continue