toc added

This commit is contained in:
Andreas Stephanides
2015-05-05 10:39:21 +02:00
parent e16762fdd0
commit 24ed5d29e3
3 changed files with 34 additions and 1 deletions

View File

@@ -101,5 +101,32 @@ class Document < ActiveRecord::Base
end
end
end
def maketoc
require "open3"
require "json"
sin,sout,serr=Open3.popen3("python #{Rails.root}/bin/maketoc.py")
sin.puts(self.text)
sin.close
b=sout.read()
c=JSON.parse(b)
l=2
f=true;
s="<ul>"
c.each do |h|
s=s+ "</li></ul></li>"*(l-h["level"]) if (h["level"]<l) and !f
s=s+" </li>" if (h["level"]==l) and !f
if (h["level"]>l)
s=s + "<ul><li>" * (h["level"]-l)
else
s=s+"<li>"
end
s=s+h["text"]
l = h["level"]
f=false
end
s=s+"</ul>"*(l-2) if (l>2)
s=s+"</ul>"
self.toc=s
end
end

View File

@@ -11,6 +11,7 @@
</div>
</div>
<%= @document.toc %>
<div style="background:white; border: thin #ccc solid; padding: 15px;-webkit-box-shadow: 10px 10px 10px 0px rgba(0,0,0,0.5); -moz-box-shadow: 10px 10px 10px 0px rgba(0,0,0,0.5); box-shadow: 10px 10px 10px 0px rgba(0,0,0,0.5);">
<h1><%= @document.name %></h1>
<div>

View File

@@ -0,0 +1,5 @@
class AddTocToDocuments < ActiveRecord::Migration
def change
add_column :documents, :toc, :text
end
end