Giter Club home page Giter Club logo

Comments (5)

senny avatar senny commented on August 21, 2024 1

Sablon was not built with that use-case in mind. The behavior would be too brittle and not clear when loops and conditions are in play. Therefore I won't introduce public API to get the output you'd like.

Having said that, you can use the internal building blocks of Sablon to extract the data you need. Following is a simple script executed against a fixture template:

path = "/Users/senny/Projects/sablon/test/fixtures/recipe_template.docx"

parser = Sablon::Parser::MailMerge.new
fields = []

Zip::File.open(path).each do |entry|
  entry_name = entry.name
  content = entry.get_input_stream.read
  if entry_name == 'word/document.xml' || entry_name =~ /word\/header\d*\.xml/ || entry_name =~ /word\/footer\d*\.xml/
    document = Nokogiri::XML(content)
    fields += parser.parse_fields(document)
  end
end

fields.map(&:expression)
# => ["=title", "=intro", "ingredients:each(ingredient)", "=ingredient.amount", "=ingredient.name", "ingredients:endEach", "=preparation", "=metadata.generator", "=details.author.name"]

Depending on your templates you might just need to parse word/document.xml.

from sablon.

senny avatar senny commented on August 21, 2024

@Xorg1984 currently there is no public API to access the fields. Also I'm not sure how easily such a function can be generalized. The fields can be nested within loops and conditionals, they can also use local variables. This makes the expression not very useful outside of it's context.

What are you trying to achieve?

from sablon.

avdovin avatar avdovin commented on August 21, 2024

my task - get all document placeholders (this will be a simple strings), show interface to set values to placeholders and then generate a new document with values
maybe parse document independently? what way you recommend?

from sablon.

avdovin avatar avdovin commented on August 21, 2024

Thanks!

from sablon.

hahmed avatar hahmed commented on August 21, 2024

@avdovin how did you get on this, did you manage to get what you want working?

I am also looking to read all merge_fields from the document so the user can fill out the fields in the interface.

Was the code in the comment sufficient? #16 (comment)

Thanks

from sablon.

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.