Exemplo n.º 1
0
def markdown_comment(*args: str, padded: bool = True) -> StringBuilder:
    sb = StringBuilder()
    if args:
        if padded:
            sb.nl()
        for arg in args:
            sb.wl(f"[//]: # ({arg})")
        if padded:
            sb.nl()
    return sb
Exemplo n.º 2
0
def xml_attributes(attributes: List[str]) -> StringBuilder:
    attributes = attributes if attributes else []
    attributes_len = len(attributes)
    sb = StringBuilder()
    if attributes_len > 1:
        sb.nl()
        for i, a in enumerate(attributes):
            sb.wl(a, addtl_indent_len=1)
    elif attributes_len == 1:
        a = attributes[0]
        sb.wt(a)
    return sb
Exemplo n.º 3
0
def csharp_parameters(parameters: List[str]) -> StringBuilder:
    parameters = parameters if parameters else []
    parameters_len = len(parameters)
    sb = StringBuilder()
    if parameters_len > 1:
        sb.nl()
        for i, p in enumerate(parameters):
            sb.wt(p, addtl_indent_len=1)
            sb.wl("," if i < parameters_len - 1 else "")
    elif parameters_len == 1:
        p = parameters[0]
        sb.wt(p)
    return sb
Exemplo n.º 4
0
def get_text(template: str,
             expressions: Dict[str, str],
             template_name: str = None) -> str:
    sb = StringBuilder()

    # Add a timestamp when the template is a markdown file.
    if template_name.casefold().endswith(".md"):
        from scriptgen.templates.markdown import markdown_autogen
        sb.wb(markdown_autogen())
        sb.nl()

    # Replace placeholders.
    # i.e. replace placeholders found in the text with values found in the expressions dictionary.
    # ex: ${SOME_KEY} → ACTUAL_VALUE
    interpolated_text = interpolate_text(template, expressions)

    # Write the interpolated text into the builder.
    sb.wl(interpolated_text)

    return str(sb)
Exemplo n.º 5
0
def get_text(template: str,
             expressions: Dict[str, str],
             template_name: str = None) -> str:
    sb = StringBuilder()

    # Add a timestamps.
    if template_name.casefold().endswith(".md"):
        from scriptgen.templates.markdown import markdown_autogen
        sb.wb(markdown_autogen())
        sb.nl()
    elif template_name.casefold().endswith(".py"):
        from scriptgen import timestamp
        sb.wls(["#!/usr/bin/env python3", "# -*- coding: utf-8 -*-"])
        sb.nl()
        sb.wl(f"# Auto-generated: {timestamp()}")
        sb.nl()

    # Replace placeholders.
    # i.e. replace placeholders found in the text with values found in the expressions dictionary.
    # ex: ${SOME_KEY} → ACTUAL_VALUE
    interpolated_text = interpolate_text(template, expressions)

    # Write the interpolated text into the builder.
    sb.wl(interpolated_text)

    return str(sb)
Exemplo n.º 6
0
 def build(self, rstrip: bool = None) -> str:
     rstrip = self.rstrip if rstrip is None else rstrip
     attributes = [f'{k}="{v}"' for k, v in self.attributes.items()]
     attributes_len = len(attributes)
     sb = StringBuilder()
     sb.wt(f"<{self.name}")
     if attributes:
         sb.wt("" if attributes_len > 1 else " ")
         sb.wb(xml_attributes(attributes), rstrip=True)
     if self.content is None:
         sb.wl(" />")
     else:
         sb.wt(">")
         if isinstance(self.content, str):
             if attributes_len > 1:
                 sb.nl()
             sb.wt(f"{self.content}",
                   addtl_indent_len=1 if attributes_len > 1 else 0)
             if attributes_len > 1:
                 sb.nl()
         elif isinstance(self.content, List) and \
                 all(isinstance(c, XmlElementBuilder) for c in self.content):
             sb.nl()
             bb = BlockBuilder()
             for c in self.content:
                 bb.wb(c, rstrip)
             sb.wb(bb)
         sb.wl(f"</{self.name}>")
     return sb.build(rstrip)