Giter VIP home page Giter VIP logo

Comments (2)

laurmaedje avatar laurmaedje commented on July 18, 2024

I am considering to support passing arbitrary content for set document(title: ..). I also plan to give the templates a facelift fairly soon, so if that didn't happen until then, I'm open to the workaround.

from templates.

denkspuren avatar denkspuren commented on July 18, 2024

@laurmaedje, I have a solution to this problem (which is also mentioned here): To use a string or content for the title of a document, the only problem is to set the title metadata properly, which has to be of type string.

I wrote a function named extractText that concatenates all strings to be found in a value of type content, string or otherwise and return it. Given the function extractText the above mentioned line of code in template.typ can be written as

set document(title: extractText(title), author: authors.map(author => author.name))

The helper function can be outsourced and imported or included in the template:

#let extractText(element) = {
  if type(element) == content {
    if element == [ ] { return " " }
    return extractText(element.fields()).trim() }
  if type(element) == dictionary { return extractText(element.values()) }
  if type(element) == array {
    return element.fold("", (res, item) => res + extractText(item))
  }
  if type(element) == bool { return "" }
  return str(element)
}

#assert.eq(extractText("hey"), "hey")
#assert.eq(extractText(12), "12")
#assert.eq(extractText(12.0), "12")
#assert.eq(extractText(12.1), "12.1")
#assert.eq(extractText(false), "")
#assert.eq(extractText(version(1,2,3)), "1.2.3")
#assert.eq(extractText((1,2,3)), "123")
#assert.eq(extractText((4,(1,"Hey",12.0),(hey: 2))), "41Hey122")
#assert.eq(extractText([This is some text.]), "This is some text.")
#assert.eq(extractText([This is _some_ text.]), "This is some text.")
#assert.eq(extractText([ Is $x^2$ an _even_ Function? ]), "Is x2 an even Function?")
#assert.eq(extractText([Is $x^2$ an _even_ Function?]), "Is x2 an even Function?")
#assert.eq(extractText([[Hey] [you]]), "[Hey] [you]")

What do you think?

from templates.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.