Class: QED::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/qedoc/document.rb,
lib/qedoc/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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/qedoc/document.rb', line 30

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.



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

def css
  @css
end

#dryrunObject

Returns the value of attribute dryrun.



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

def dryrun
  @dryrun
end

#outputObject

Ouput file.



27
28
29
# File 'lib/qedoc/document.rb', line 27

def output
  @output
end

#pathsObject

Returns the value of attribute paths.



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

def paths
  @paths
end

#quietObject

Returns the value of attribute quiet.



24
25
26
# File 'lib/qedoc/document.rb', line 24

def quiet
  @quiet
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#copy_support_filesObject



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

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.



47
48
49
50
51
52
53
54
55
# File 'lib/qedoc/document.rb', line 47

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.



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

def generate
  copy_support_files

  output = ''
  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

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

=begin
    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
=end

    output << "#{body}\n"
  end

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

  save(html)
end

#make_output_directoryObject



151
152
153
# File 'lib/qedoc/document.rb', line 151

def make_output_directory
  FileUtils.mkdir_p(output)
end

#quiet?Boolean

Supress output.

Returns:

  • (Boolean)


58
# File 'lib/qedoc/document.rb', line 58

def quiet? ; @quiet ; end

#save(text) ⇒ Object

Save specification document.



140
141
142
143
144
145
146
147
148
149
# File 'lib/qedoc/document.rb', line 140

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.



132
133
134
135
136
137
# File 'lib/qedoc/document.rb', line 132

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