samedi 26 décembre 2015

Rails seeding db from docx files

I have to make an dictionary-like web app for my university organization and I ran into some problems. They sent me Terms for dictionary in .docx files and I am not sure how to insert them into Term model in my app. Docx. files looks something like this: enter image description here

Is there a possibility do easilly do It?
My Term model has phrase:string and explanation:text attributes.


This is my seed.rb file:

f = File.open(File.join(Rails.root, "/app/assets/seed/seed2.docx"))
while !f.eof do
  phrase = ''
  explanation = ''
  f.each_char do |c|
    if c == "–" #Separating Terms from its explanations.
      break
    end
    phrase << c
  end
  phrase[-1] = '' #delete ' '(space) from the end.
  f.each_line do |l|
    if l.to_s.strip.length == 0 # Check if new-line
      break
    end
    explanation << l
  end
  explanation[0] = '' #delete ' '(space) from the start.
  Term.create!(phrase: phrase, explanation: explanation)
end
f.close

I managed to seed files with pure text and display it using

<%= term.phrase %> -
<%= simple_format(term.explanation) %>

but I don't know how to import the tables and if is there a possibility to display the dotted lists from docx files? Please help, I feel like it is too big of a deal for me and I am not sure I can manage to implement such dictionary seeding from docx. I will appreaciate any help!

Aucun commentaire:

Enregistrer un commentaire