/
doc_controller.rb
155 lines (132 loc) · 4.34 KB
/
doc_controller.rb
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'redcarpet'
class DocController < ApplicationController
layout "layout"
def index
@book = Book.where(:code => 'en').first
end
def ref
end
def blog
y = params[:year]
m = params[:month]
d = params[:day]
slug = params[:slug]
file = "#{y}-#{m}-#{d}-#{slug}"
@path = path = "#{Rails.root}/app/views/blog/progit/#{file}"
content = ''
if File.exists?("#{path}.markdown")
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
content = File.read("#{path}.markdown")
content, @frontmatter = extract_frontmatter(content)
@content = markdown.render(content)
elsif File.exists?("#{path}.html")
content = File.read("#{path}.html")
@content, @frontmatter = extract_frontmatter(content)
end
end
def extract_frontmatter(content)
if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m
cnt = $3
data = YAML.load($1)
[cnt, data]
else
[content, {}]
end
end
def test
render 'doc/rebase'
end
def man
if params[:version]
doc_version = DocVersion.for_version(params[:file], params[:version])
else
doc_version = DocVersion.latest_for(params[:file])
end
if doc_version.nil?
redirect_to :ref
else
@versions = DocVersion.version_changes(params[:file], 20)
@last = DocVersion.last_changed(params[:file])
@related = DocVersion.get_related(params[:file], 8)
@version = doc_version.version
@file = doc_version.doc_file
@doc = doc_version.doc
end
end
def book
lang = params[:lang] || 'en'
@book = Book.where(:code => lang).first
end
def book_section
lang = params[:lang]
slug = params[:slug]
@content = Book.where(:code => lang).first.sections.where(:slug => slug).first
@related = @content.get_related(8)
end
CMD_GROUPS = [
['Setup and Config', [ 'config', 'help' ]],
['Getting and Creating Projects', [ 'init', 'clone']],
['Basic Snapshotting', [ 'add', 'status', 'diff', 'commit', 'reset', 'rm', 'mv']],
['Branching and Merging', [ 'branch', 'checkout', 'merge', 'mergetool', 'log', 'stash', 'tag' ]],
['Sharing and Updating Projects', [ 'fetch', 'pull', 'push', 'remote', 'submodule' ]],
['Inspection and Comparison', [ 'show', 'log', 'diff', 'shortlog', 'describe' ]],
['Patching', ['am', 'apply', 'cherry-pick', 'rebase']],
['Debugging', [ 'bisect', 'blame' ]],
['Email', ['am', 'apply', 'format-patch', 'send-email', 'request-pull']],
['External Sytems', ['svn', 'fast-import']],
['Administration', [ 'gc', 'fsck', 'reflog', 'filter-branch', 'instaweb', 'archive' ]],
['Server Admin', [ 'daemon', 'update-server-info' ]],
]
# commands index
def commands
@related = {}
ri = RelatedItem.where(:content_type => 'reference', :related_type => 'book')
ri.each do |item|
cmd = item.name.gsub('git-', '')
if s = Section.where(:slug => item.related_id).first
@related[cmd] ||= []
@related[cmd] << [s.cs_number, s.slug, item.score]
end
end
@groups = CMD_GROUPS
end
# so we can display urls old progit.org style
def progit
chapter = params[:chapter].to_i
section = params[:section].to_i
lang = params[:lang] || 'en'
book = Book.where(:code => lang).first
chapter = book.chapters.where(:number => chapter).first
@content = chapter.sections.where(:number => section).first
render 'book_section'
end
def book_update
if params[:token] != ENV['UPDATE_TOKEN']
return render :text => 'nope'
end
lang = params[:lang]
chapter = params[:chapter].to_i
section = params[:section].to_i
chapter_title = params[:chapter_title]
section_title = params[:section_title]
content = params[:content].force_encoding("UTF-8")
# create book (if needed)
book = Book.where(:code => lang).first_or_create
# create chapter (if needed)
chapter = book.chapters.where(:number => chapter).first_or_create
chapter.title = chapter_title
chapter.save
# create/update section
section = chapter.sections.where(:number => section).first_or_create
section.title = section_title
section.html = content
section.save
# TODO: find and index commands
render :text => 'ok'
end
# API Methods to update book content #
def videos
end
def ext
end
end