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 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)