Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter / printer template support for the document, section and content blocks #262

Open
traut opened this issue Nov 14, 2024 · 0 comments
Labels
enhancement New feature or request formatting
Milestone

Comments

@traut
Copy link
Member

traut commented Nov 14, 2024

Background

Fabric must support custom formatting of specific elements and the documents, to allow for custom corporate styling. Since the content blocks / documents can be rendered into various formats (Markdown, HTML, etc), the format template should be format-specific.

This issue surpasses #240 as much more flexible approach

Design

We introduce format <format> block:

  format html {
    value = <<-EOT
    <div class="section">
      <h3>{{ .title.as_text }}</h3>
      <p class="font-mono text-sm">{{ .content.text.foo.as_text }}</p>

      <div class="subsection">
        {{ .section.subsec.as_html }}
      </div>
    </div>
    EOT
  }
  • different formats might support different arguments (for example, DOCX format will have template_file argument)
  • there can be only one format <format> block per format on the same level (root level, section block, content block)
  • the format block defines the format of the block in which it is defined
  • the format template can access the data context and all subtrees of rendered content

Example

format html "custom_section" {
  template = <<-EOT
    <div class="section">
      <h2>{{ .content.title.as_text }}</h2>
      <p>{{ .content.text.bar.as_text }}</p>
    </div>
  EOT
}


document "test-doc" {

  vars {
     x = 1
  }

  title = "Doc title"

  content text "foo" {
    value = "Some content text"
  }

  section "sec1" {
    title = "Section title 1"

    content text "bar" {
      value = "Nested content inside a section 1"
    }

    format html ref {
      base = format.html.custom_section
    }
  }

  section "sec2" {
    title = "Section title 2"

    content text "baz" {
      value = "Nested content inside a section 2"
    }

    format html ref {
      base = format.html.custom_section
    }
  }

  format html {
    template = <<-EOT
      <!DOCTYPE html>
      <html lang="en" class="h-full text-black bg-white dark:bg-raisinBlack dark:text-raisinBlack-50">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <title id="page-title">{{ .content.title.as_text }}</title>

        <link href="./styles.css" rel="stylesheet"/>
      </head>
      <body class="h-full">

      <h1>{{ .content.title.as_text }} with {{ .vars.x }}</h1>

      <p>{{ .content.text.foo.as_text }}</p>

      <!-- Specifying exact sections or looping over all sections -->
      {{ .section.sec1.as_html }}
      {{ .section.sec2.as_html }}

      </body>
      </html>
    EOT
  }

  format docx {
      template_file = "./template.docx"
  }

}

References

@traut traut added enhancement New feature or request formatting labels Nov 14, 2024
@traut traut added this to the v0.5 milestone Nov 14, 2024
@traut traut changed the title Printer template support for the document, section and content blocks Formatter / printer template support for the document, section and content blocks Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request formatting
Projects
None yet
Development

No branches or pull requests

1 participant