<commit_before>import DoctorPretty
import Html
import Operadics

public func prettyPrint(node: Node, pageWidth: Int = 110) -> String {

  return (prettyPrint(node: node) as Doc)
    .renderPretty(ribbonFrac: 1, pageWidth: pageWidth)
    .displayString()
}

public func prettyPrint(nodes: [Node], pageWidth: Int = 110) -> String {

  return nodes.map(prettyPrint(node:))
    .vcat()
    .renderPretty(ribbonFrac: 1, pageWidth: pageWidth)
    .displayString()
}

private func prettyPrint(node: Node) -> Doc {
  switch node {
  case let .element(element):
    return prettyPrint(element: element)
  case let .text(text):
    return .text(text.string)
  case let .comment(comment):
    return prettyPrint(comment: comment)
  case let .document(nodes):
    return prettyPrint(document: nodes)
  }
}

private func prettyPrint(element: Element) -> Doc {

  return prettyPrintOpenTag(element: element)
    <> (
      element.content.map {
        $0.map(prettyPrint(node:))
          .vcat()
          .indent(2)
        } ?? .zero
    )
    <> prettyPrintCloseTag(element: element)
}

private func prettyPrintOpenTag(element: Element) -> Doc {

  return .text("<")
    <> .text(element.name)
    <> prettyPrint(attributes: element.attribs)
    <> (element.content == nil ? .zero : .text(">") <> .hardline)
}

private func prettyPrintCloseTag(element: Element) -> Doc {
  return element.content == nil
    ? .text(" />")
    : .hardline <> .text("</") <> .text(element.name) <> .text(">")
}

private func prettyPrint(attributes attribs: [Attribute]) -> Doc {

  return (attribs.count == 0 ? .text("") : .text(" "))
    <%%> attribs
      .map(prettyPrint(attribute:))
      .sep()
      .hang(0)
}

private func prettyPrint(attribute: Attribute) -> Doc {
  return .text(attribute.value.render(with: attribute.key)?.string ?? "")
}

private func prettyPrint(comment: String) -> Doc {
  return Doc.text("<!--")
    <%> comment
      .split(separator: " ")
      .map(String.init)
      .map(Doc.text)
      .fillSep()
      .hang(0)
    <%> Doc.text("-->")
}

private func prettyPrint(document nodes: [Node]) -> Doc {
  return Doc.text("<!DOCTYPE html>")
    <> .hardline
    <> nodes.map(prettyPrint(node:)).vcat()
}
<commit_msg>clean<commit_after>import DoctorPretty
import Html
import Operadics

public func prettyPrint(node: Node, pageWidth: Int = 110) -> String {

  return (prettyPrint(node: node) as Doc)
    .renderPretty(ribbonFrac: 1, pageWidth: pageWidth)
    .displayString()
}

public func prettyPrint(nodes: [Node], pageWidth: Int = 110) -> String {

  return nodes.map(prettyPrint(node:))
    .vcat()
    .renderPretty(ribbonFrac: 1, pageWidth: pageWidth)
    .displayString()
}

private func prettyPrint(node: Node) -> Doc {
  switch node {
  case let .element(element):
    return prettyPrint(element: element)
  case let .text(text):
    return .text(text.string)
  case let .comment(comment):
    return prettyPrint(comment: comment)
  case let .document(nodes):
    return prettyPrint(document: nodes)
  }
}

private func prettyPrint(element: Element) -> Doc {

  return prettyPrintOpenTag(element: element)
    <> (
      element.content.map {
        $0.map(prettyPrint(node:))
          .vcat()
          .indent(2)
        } ?? .zero
    )
    <> prettyPrintCloseTag(element: element)
}

private func prettyPrintOpenTag(element: Element) -> Doc {

  return .text("<")
    <> .text(element.name)
    <> prettyPrint(attributes: element.attribs)
    <> (element.content == nil ? .zero : .text(">") <> .hardline)
}

private func prettyPrintCloseTag(element: Element) -> Doc {
  return element.content == nil
    ? .text(" />")
    : .hardline <> .text("</") <> .text(element.name) <> .text(">")
}

private func prettyPrint(attributes attribs: [Attribute]) -> Doc {

  return (attribs.count == 0 ? .text("") : .text(" "))
    <%%> attribs
      .map(prettyPrint(attribute:))
      .sep()
      .hang(0)
}

private func prettyPrint(attribute: Attribute) -> Doc {
  return .text(attribute.value.render(with: attribute.key)?.string ?? "")
}

private func prettyPrint(comment: String) -> Doc {
  return .text("<!--")
    <%> comment
      .split(separator: " ")
      .map(String.init)
      .map(Doc.text)
      .fillSep()
      .hang(0)
    <%> .text("-->")
}

private func prettyPrint(document nodes: [Node]) -> Doc {
  return .text("<!DOCTYPE html>")
    <> .hardline
    <> nodes.map(prettyPrint(node:)).vcat()
}

{'fraction_non_alphanumeric': 0.16549853040922452, 'fraction_numerical': 0.005878363101966991, 'mean_word_length': 3.209324452901998, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '956640', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1423, 'n_words': 322}