def csharp_usings(*args: str, **kwargs: str) -> StringBuilder: sb = StringBuilder() for arg in args: sb.wl(f"using {arg};") for k, v in kwargs.items(): sb.wl(f"using {k} = {v};") return sb
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)
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)
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
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
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
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)
def csharp_doc(*args: str) -> StringBuilder: sb = StringBuilder() sb.wl("/// <summary>") for arg in args: sb.wl(f"/// {sb.indent}{arg}") sb.wl("/// </summary>") return sb
def csharp_comment(*args: str) -> StringBuilder: sb = StringBuilder() for arg in args: sb.wl(f"// {arg}") return sb
def xml_declaration(version: str = "1.0", encoding: str = "UTF-8") -> StringBuilder: sb = StringBuilder() sb.wl(f'<?xml version="{version}" encoding="{encoding}" ?>') return sb
def xml_comment(*args: str) -> StringBuilder: sb = StringBuilder() if args: for arg in args: sb.wl(f"<!-- {arg} -->") return sb