aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tasks/userdoc.rake
blob: 626320519a88b7538c95530548a7eae42c41598e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#encoding: utf-8 
if Rails.env != 'production'
require 'fileutils'
require 'nokogiri'

namespace :doc do
  desc "doc production"
  task :user  do
    puts "Produce user documentation"
    
    abort("Missing commands: please install them before processing") unless command?("pandoc") && command?("unzip") && command?("zip")

    FileUtils.rm_r("tmp/doc") if File.exist?("tmp/doc")
    FileUtils.rm_r("word") if File.exist?("word")
    File.delete("userdoc.docx") if File.exists?("userdoc.docx")
 
    Dir.mkdir("tmp/doc");
    # merge all textile files in one
    merge_textiles
    
    # copy images in temp dir
    puts "add images"
    FileUtils.cp_r Dir.glob("public/help/*.png"), "tmp/doc"
    
    # call pandoc to build docx
    puts "build docx"
    Dir.chdir "tmp/doc"
    system "pandoc -s -o ../../userdoc.docx temp.textile"
    Dir.chdir "../.."
    
    if !File.exists?("userdoc.docx")
      puts "pandoc failed to produce document"
    else  
      # patch docx
      patch_docx "userdoc.docx"
      
      # clean working directory
      puts "clean temp files"
      FileUtils.rm_r("tmp/doc") if File.exist?("tmp/doc")
      
      # end job
      puts "User doc completed"
    end
    
  end
  
  def merge_textiles
    puts "parsing app/views/help/toc.textile"
    File.open('tmp/doc/temp.textile','w') { |f| 
      File.open('app/views/help/toc.textile','r').each_line do |line|
        line.chomp!
        if line.start_with?("#")
          title=""
          file=""
          if line.include? ":"
            title=line.scan(/"([^"]*)"/).last.first
            file=line.split(":").last
          else
            title=line.split.last
          end
          if line.start_with?("##")
            f.puts "h2. "+title
          elsif line.start_with?("#")
            f.puts "h1. "+title
          end
          f.puts ""
          if !file.empty?
            parse_textile "app/views/help/#{file}.textile",f
          end
        end
      end
    }
  end
  
  def command?(command)
       puts "command #{command} missing" unless system("which #{command} > /dev/null 2>&1")  
       system("which #{command} > /dev/null 2>&1")
  end
  
  def parse_textile (file,out)
    puts "  parsing #{file}"
    File.open(file,"r").each_line do |l|
      l.chomp!
      next if l.start_with? "---" 
      next if l.start_with? "layout:" 
      next if l.start_with? "title:"
      next if !l.scan(/^\* ".*":#/).empty?
      l=l.gsub("->","→").gsub("<<","«").gsub(">>","»").gsub("oe","œ").gsub("p=.","p.")
      clean_local_links l
      out.puts l 
    end
    out.puts ""
  end

  def clean_local_links (line)
    if line.include? '":'
      check_and_remove_link line,0
    end
  end
  
  def check_and_remove_link (line,pos)
      link_pos = line.index '":',pos
      return if link_pos.nil? 
      title_pos = line[0..link_pos-1].rindex '"'
      title=line[title_pos+1..link_pos-1]
      if !line[link_pos+2..-1].start_with? "http"
        end_link = line.index(/[^a-z_]/, link_pos+2)
        end_link = line.length if end_link.nil? 
        end_link = end_link - 1
        link=line[link_pos+1..end_link]
        # remove link syntax
        line.sub!(link,'')
        line.sub!('"'+title+'"',title)
      end
      check_and_remove_link line,link_pos+1
  end  
  
  def patch_docx file
    system "unzip #{file} word/document.xml > /dev/null 2>&1"
    
    f=File.open("word/document.xml","r+")
    doc = Nokogiri::XML(f)
    doc.xpath("//w:t").each do |tag|
      if tag.to_s.include? '&amp;#95;'
        tag.child.content = tag.child.content.gsub '&#95;','_'
      end
    end
    f.rewind
    f.write(doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML))
    f.close
    
    system "zip -r #{file} word/document.xml > /dev/null 2>&1"
    FileUtils.rm_r("word") if File.exist?("word")
  end
  
end

end