Class: QED::Document

Inherits:
Object show all
Defined in:
lib/qed/document.rb,
lib/qed/cli/qedoc.rb,
lib/qed/document/markup.rb

Overview

Document

TODO: css and javascripts have fixed location need to make more flexible. TODO: Have option to run documents through the runner and color code output; need htmlreporter.

Defined Under Namespace

Classes: Markup

Constant Summary collapse

DEFAULT_TITLE =
"Demonstration"
DEFAULT_CSS =

“../assets/styles/spec.css”

nil
DEFAULT_OUTPUT =
"qed.html"
DEFAULT_PATH =
"qed"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Document

New Spec Document object.


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/qed/document.rb', line 43

def initialize(options={})
  options.each do |k,v|
    __send__("#{k}=", v)
  end

  @paths  ||= []

  @output ||= DEFAULT_OUTPUT
  @title  ||= DEFAULT_TITLE
  @css    ||= DEFAULT_CSS

  if File.directory?(@output)
    @output = File.join(@output, 'qed.html')
  end

  @format ||= File.extname(@output).sub('.','')

  if @paths.empty?    #dir = Dir['{test/demos,demos,demo}'].first || DEFAULT_PATH
    #@paths  = File.join(dir, '**', '*')

    abort "No files to document."
  end
end

Instance Attribute Details

#cssObject

Returns the value of attribute css


21
22
23
# File 'lib/qed/document.rb', line 21

def css
  @css
end

#dryrunObject

Returns the value of attribute dryrun


23
24
25
# File 'lib/qed/document.rb', line 23

def dryrun
  @dryrun
end

#formatObject

Format of output file, either 'html' or 'plain'. Defaults to extension of output file.


32
33
34
# File 'lib/qed/document.rb', line 32

def format
  @format
end

#outputObject

Ouput file.


28
29
30
# File 'lib/qed/document.rb', line 28

def output
  @output
end

#pathsObject

Files to document.


35
36
37
# File 'lib/qed/document.rb', line 35

def paths
  @paths
end

#quietObject

Returns the value of attribute quiet


25
26
27
# File 'lib/qed/document.rb', line 25

def quiet
  @quiet
end

#titleObject

Returns the value of attribute title


19
20
21
# File 'lib/qed/document.rb', line 19

def title
  @title
end

Class Method Details

.cli(*argv) ⇒ Object

Command line interface for generating qedocs.


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
# File 'lib/qed/cli/qedoc.rb', line 9

def self.cli(*argv)

  options = {}

  parser = OptionParser.new do |usage|
    usage.banner = "Usage: qedoc [OPTIONS] <QEDFile1> [ <QEDFile2> ... ]"

    usage.on("-o", "--output [DIR]", "Output directory") do |dir|
      options[:output]= dir
    end

    usage.on("-t", "--title [TITLE]", "Title of Document") do |title|
      options[:title]= title
    end

    usage.on("--css [URI]", "Specify a URI for a CSS file add to HTML header.") do |uri|
      options[:css] = uri
    end

    usage.on("--dryrun", "") do
      options[:dryrun] = true
    end

    usage.on("-q", "--quiet", "") do
      options[:quiet] = true
    end

    usage.on_tail("-h", "--help", "display this help message") do
      puts usage
      exit
    end
  end

  parser.parse!(argv)

  options[:paths] = argv.dup

  #opts[:output] = cli.options[:file]
  #opts[:dryrun] = cli.options[:dryrun]
  #opts[:quiet]  = cli.options[:quiet]
  #opts[:css]    = cli.options[:css]
  #opts[:title]  = cli.options[:title]

  doc = QED::Document.new(options)

  doc.generate
end

Instance Method Details

#demo_filesObject

Demo files.


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/qed/document.rb', line 68

def demo_files
  @demo_files ||= (
    files = []
    paths.each do |f|
      if File.directory?(f)
        files.concat Dir[File.join(f,'**','*')]
      else
        files.concat Dir[f]
      end
    end
    files = files.reject{ |f| File.directory?(f) }
    files = files.reject{ |f| File.extname(f) == '.rb' }
    files = files.reject{ |f| /(fixtures|helpers)\// =~ f }

    # doesn't include .rb applique but does markup applique
    applique, files = files.partition{ |f| /applique\// =~ f }

    applique.sort + files.sort
  )
end

#file_type(text) ⇒ Object (private)


217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/qed/document.rb', line 217

def file_type(text)
  rdoc = text.index(/^\=/)
  markdown = text.index(/^\#/)
  if markdown && rdoc
    rdoc < markdown ? '.rdoc' : '.markdown'
  elsif rdoc
    '.rdoc'
  elsif markdown
    '.markdown'
  else  # fallback to rdoc
    '.rdoc'
  end
end

#generateObject

Generate specification document.

– TODO: Use Malt to support more formats in future. ++


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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/qed/document.rb', line 99

def generate  #copy_support_files


  out   = ''
  files = []

  #paths.each do |path|
  #  files.concat(Dir.glob(path).select{ |f| File.file?(f) })
  #end
  #files.sort!

  if dryrun or $DEBUG
    puts demo_files.sort.join(" ")
  end

  demo_files.each do |file|
    #strio = StringIO.new('')
    #reporter = Reporter::Html.new(strio)
    #runner = Runner.new([file], reporter)
    #runner.check
    #iotext = strio.string
    #strio.close

    ext = File.extname(file)
    txt = File.read(file)

    if ext == '.qed'
      ext = file_type(txt)
    end

    #text = Tilt.new(file).render
    #html = Nokogiri::HTML(text)
    #body = html.css("body")

    text = ""
    case ext    #when '.qed'
    #  require_qedoc
    #  markup = Markup.new(File.read(file))
    #  text << markup.to_html

    when '.rd', '.rdoc'
      require_rdoc
      require_qedoc
      if html?
        markup = Markup.new(txt)
        text << markup.to_html        #text << markup.convert(iotext, formatter)

      else
        text << txt
      end        
    when '.md', '.markdown'
      require_rdiscount
      if html?
        markdown = RDiscount.new(txt)
        text << markdown.to_html
      else
        text << txt
      end
    end

    # TODO: Use Nokogiri to find all <pre>'s with preceeding <p>'s that have text ending in `:`, and
    # add the class `no-highlight`. If no preceeding `:` add class ruby.

    out << "#{text}\n"
  end

  if html?
    temp = Template.new(template, out, title, css)
    html = temp.parse_template
    save(html)
  else
    save(out)
  end
end

#html?Boolean

Returns:

  • (Boolean)

175
176
177
# File 'lib/qed/document.rb', line 175

def html?
  format == 'html'
end

#make_output_directoryObject


209
210
211
212
# File 'lib/qed/document.rb', line 209

def make_output_directory
  dir = File.dirname(output)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)
end

#quiet?Boolean

Supress output.

Returns:

  • (Boolean)

90
91
92
# File 'lib/qed/document.rb', line 90

def quiet?
  @quiet
end

#require_qedocObject (private)


232
233
234
235
236
237
# File 'lib/qed/document.rb', line 232

def require_qedoc
  @require_qedoc ||= (
    require 'qed/document/markup'
    true
  )
end

#require_rdiscountObject (private)


254
255
256
257
258
259
# File 'lib/qed/document.rb', line 254

def require_rdiscount
  @require_rdiscount ||= (
    require 'rdiscount'
    true
  )
end

#require_rdocObject (private)


240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/qed/document.rb', line 240

def require_rdoc
  @require_rdoc ||= (
    begin
      require 'rdoc/markup/to_html'
    rescue LoadError
      require 'rubygems'
      gem 'rdoc'
      retry
    end
    true
  )
end

#save(text) ⇒ Object

Save specification document.


197
198
199
200
201
202
203
204
205
206
207
# File 'lib/qed/document.rb', line 197

def save(text)
  if dryrun
    puts "[dry-run] Write #{output}" unless quiet
  else
    make_output_directory
    File.open(output, 'wb') do |f|
      f << text
    end
    puts "Write #{output}" unless quiet
  end
end

#templateObject

Load specification HTML template.


189
190
191
192
193
194
# File 'lib/qed/document.rb', line 189

def template
  @template ||= (
    file = File.join(File.dirname(__FILE__), 'document', 'template.rhtml')
    File.read(file)
  )
end