Class: QED::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/qed/document.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 =
"doc/qedoc"
DEFAULT_PATH =
"test/demos"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Document

New Spec Document object.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/qed/document.rb', line 28

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

  @title  ||= DEFAULT_TITLE
  @css    ||= DEFAULT_CSS
  @output ||= DEFAULT_OUTPUT
  @paths  ||= []

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

Instance Attribute Details

#cssObject

Returns the value of attribute css.



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

def css
  @css
end

#dryrunObject

Returns the value of attribute dryrun.



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

def dryrun
  @dryrun
end

#outputObject

Ouput file.



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

def output
  @output
end

#pathsObject

Returns the value of attribute paths.



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

def paths
  @paths
end

#quietObject

Returns the value of attribute quiet.



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

def quiet
  @quiet
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#copy_support_filesObject



114
115
116
117
118
119
120
# File 'lib/qed/document.rb', line 114

def copy_support_files
  make_output_directory
  %w{jquery.js}.each do |fname|
    file = File.join(File.dirname(__FILE__), 'document', fname)
    FileUtils.cp(file, output)
  end
end

#demo_filesObject

Demo files.



45
46
47
48
49
50
51
52
53
# File 'lib/qed/document.rb', line 45

def demo_files
  @demo_files ||= (
    glob = paths.map{ |f| File.directory?(f) ? Dir["#{f}/**/*"] : Dir[f] }.flatten
    glob = glob.select do |f|
      File.file?(f) && f !~ /fixtures\/|helpers\// && f !~ /\.rb$/
    end
    glob.sort
  )
end

#generateObject

Generate specification document.



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
# File 'lib/qed/document.rb', line 59

def generate
  copy_support_files

  text  = ''
  files = []

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

  #TODO: load .config/qedrc.rb

  demo_files.each do |file|
    puts file unless quiet?

    #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

    case ext
    #when '.qed'
    #  require_qedoc
    #  markup = Markup.new(File.read(file))
    #  text << markup.to_html
    when '.rd', '.rdoc'
      require_rdoc
      markup = RDoc::Markup::ToHtml.new
      text << markup.convert(txt)
      #text << markup.convert(iotext, formatter)
    when '.md', '.markdown'
      require_rdiscount
      markdown = RDiscount.new(txt)
      text << markdown.to_html
    end
    text << "\n"
  end

  temp = Template.new(template, text, title, css)
  html = temp.parse_template

  save(html)
end

#make_output_directoryObject



142
143
144
# File 'lib/qed/document.rb', line 142

def make_output_directory
  FileUtils.mkdir_p(output)
end

#quiet?Boolean

Supress output.

Returns:

  • (Boolean)


56
# File 'lib/qed/document.rb', line 56

def quiet? ; @quiet ; end

#save(text) ⇒ Object

Save specification document.



131
132
133
134
135
136
137
138
139
140
# File 'lib/qed/document.rb', line 131

def save(text)
  if dryrun
    puts "\nwrite #{output}"
  else
    make_output_directory
    File.open(output + '/index.html', 'wb') do |f|
      f << text
    end
  end
end

#templateObject

Load specification HTML template.



123
124
125
126
127
128
# File 'lib/qed/document.rb', line 123

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