Skip to content
This repository
Browse code

Preindex and Asciidoc updates

* Change doc.blob_sha from text to string
* Support [NOTE] admonition blocks
* Use the SHA1 of the assembled (ie. with includes) doc so changes
  to included files will cause new content to generate.
  • Loading branch information...
commit 32339b969b8dad4511899519b1d211031562a4d8 1 parent 8331570
Nick Hengeveld authored April 17, 2012 schacon committed May 02, 2012
9  db/migrate/20120417220111_change_doc_sha_to_string.rb
... ...
@@ -0,0 +1,9 @@
  1
+class ChangeDocShaToString < ActiveRecord::Migration
  2
+  def up
  3
+    change_column :docs, :blob_sha, :string
  4
+  end
  5
+
  6
+  def down
  7
+    change_column :docs, :blob_sha, :text
  8
+  end
  9
+end
2  db/schema.rb
@@ -47,7 +47,7 @@
47 47
   end
48 48
 
49 49
   create_table "docs", :force => true do |t|
50  
-    t.text      "blob_sha"
  50
+    t.string    "blob_sha"
51 51
     t.text      "plain"
52 52
     t.text      "html"
53 53
     t.timestamp "created_at", :null => false
10  lib/asciidoc.rb
@@ -48,6 +48,7 @@ module Asciidoc
48 48
     :name     => /^([A-Za-z].*)\s*$/,
49 49
     :line     => /^([=\-~^\+])+\s*$/,
50 50
     :verse    => /^\[verse\]\s*$/,
  51
+    :note     => /^\[NOTE\]\s*$/,
51 52
     :dlist    => /^(.*)(::|;;)\s*$/,
52 53
     :olist    => /^(\d+\.|\. )(.*)$/,
53 54
     :ulist    => /^\s*[\*\-]\s+(.*)$/,
@@ -864,6 +865,15 @@ def next_block(lines, parent=self)
864 865
             end
865 866
 
866 867
             block = Block.new(parent, :verse, buffer)
  868
+          elsif this_line.match(REGEXP[:note])
  869
+            # note is an admonition preceded by [NOTE] and lasts until a blank line
  870
+            this_line = lines.shift
  871
+            while !this_line.nil? && !this_line.strip.empty?
  872
+              buffer << this_line
  873
+              this_line = lines.shift
  874
+            end
  875
+
  876
+            block = Block.new(parent, :note, buffer)
867 877
           elsif this_line.match(REGEXP[:listing])
868 878
             # listing is surrounded by '----' (3 or more dashes) lines
869 879
             this_line = lines.shift
54  lib/tasks/index.rake
... ...
@@ -1,13 +1,21 @@
1 1
 require 'asciidoc'
2 2
 require 'octokit'
3 3
 require 'time'
  4
+require 'digest/sha1'
4 5
 
5 6
 # fill in the db from a local git clone
6 7
 task :preindex => :environment do
  8
+  ActiveRecord::Base.logger.level = Logger::WARN
  9
+
7 10
   template_dir = File.join(Rails.root, 'templates')
8 11
   repo = ENV['GIT_REPO'] || 'git/git'
9 12
   rerun = false
10 13
 
  14
+  blob_content = Hash.new do |blobs, sha|
  15
+    content = Base64.decode64( Octokit.blob( repo, sha, :encoding => 'base64' ).content )
  16
+    blobs[sha] = content.encode( 'utf-8', :undef => :replace )
  17
+  end
  18
+
11 19
   # find all tags
12 20
   tags = Octokit.tags( repo ).select { |tag| tag.name =~ /^v1[\d\.]+$/ }  # just get release tags
13 21
 
@@ -25,7 +33,7 @@ task :preindex => :environment do
25 33
     commit_info = Octokit.commit( repo, tag.name )
26 34
     commit_sha = commit_info.sha
27 35
     tree_sha = commit_info.commit.tree.sha
28  
-    ts = Time.parse( commit_info.commit.committer.date ).to_i
  36
+    ts = Time.parse( commit_info.commit.committer.date )
29 37
 
30 38
     # save metadata
31 39
     puts "#{tag.name}: #{ts}, #{commit_sha[0, 8]}, #{tree_sha[0, 8]}"
@@ -41,11 +49,6 @@ task :preindex => :environment do
41 49
 
42 50
     puts "Found #{doc_files.size} entries"
43 51
 
44  
-    blob_content = Hash.new do |docs, sha|
45  
-      content = Base64.decode64( Octokit.blob( repo, sha, :encoding => 'base64' ).content )
46  
-      content.encode( 'utf-8', :undef => :replace )
47  
-    end
48  
-
49 52
     # Generate this tag's command list for includes
50 53
     if cmd_file = tag_files.detect { |ent| ent.path == 'command-list.txt' }
51 54
       commands = blob_content[cmd_file.sha]
@@ -77,28 +80,31 @@ task :preindex => :environment do
77 80
     doc_files.each do |entry|
78 81
       path = File.basename( entry.path, '.txt' )
79 82
       file = DocFile.where(:name => path).first_or_create
80  
-      doc = Doc.where(:blob_sha => entry.sha).first_or_create
81  
-      if !doc.plain || !doc.html
82  
-        content = blob_content[entry.sha]
83  
-        asciidoc = Asciidoc::Document.new(path, content) do |inc|
84  
-          if categories.has_key?(inc)
85  
-            categories[inc]
  83
+
  84
+      content = blob_content[entry.sha]
  85
+      asciidoc = Asciidoc::Document.new(path, content) do |inc|
  86
+        if categories.has_key?(inc)
  87
+          categories[inc]
  88
+        else
  89
+          if match = inc.match(/^\.\.\/(.*)$/)
  90
+            git_path = match[1]
86 91
           else
87  
-            if match = inc.match(/^\.\.\/(.*)$/)
88  
-              git_path = match[1]
89  
-            else
90  
-              git_path = "Documentation/#{inc}"
91  
-            end
92  
-
93  
-            if inc_file = tag_files.detect { |f| f.path == git_path }
94  
-              blob_content[inc_file.sha]
95  
-            else
96  
-              ''
97  
-            end
  92
+            git_path = "Documentation/#{inc}"
  93
+          end
  94
+
  95
+          if inc_file = tag_files.detect { |f| f.path == git_path }
  96
+            blob_content[inc_file.sha]
  97
+          else
  98
+            ''
98 99
           end
99 100
         end
  101
+      end
  102
+      asciidoc_sha = Digest::SHA1.hexdigest( asciidoc.source )
  103
+
  104
+      doc = Doc.where( :blob_sha => asciidoc_sha ).first_or_create
  105
+      if !doc.plain || !doc.html
100 106
         doc.plain = asciidoc.source
101  
-        doc.html  = asciidoc.render(template_dir)
  107
+        doc.html  = asciidoc.render( template_dir )
102 108
         doc.save
103 109
       end
104 110
       DocVersion.where(:version_id => stag.id, :doc_id => doc.id, :doc_file_id => file.id).first_or_create
8  templates/section_note.html.erb
... ...
@@ -0,0 +1,8 @@
  1
+<div class="admonitionblock">
  2
+  <table>
  3
+    <tr>
  4
+      <td class="icon"><div class="title">Note</div></td>
  5
+      <td class="content"><%= content %></td>
  6
+    </tr>
  7
+  </table>
  8
+</div>

0 notes on commit 32339b9

Please sign in to comment.
Something went wrong with that request. Please try again.