Class: Geb::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/geb/page.rb

Defined Under Namespace

Classes: FailedToOutputPage, PageFileNotFound, PageFileReadFailure, PageMissingTemplateDefition

Constant Summary collapse

VARIABLE_PATTERN =

variable pattern match

/_\{(.*?)\}_/
PAGE_TITLE_PATTERN =
/<title>(.*?)<\/title>/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, path) ⇒ Geb::Page

page constructor, initializes the page object and attributes

Parameters:

  • site (Geb::Site)

    the site object

  • path (String)

    the page path

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/geb/page.rb', line 70

def initialize(site, path)

  # set the site and path
  @site = site
  @path = path

  # check if the page file exists
  raise PageFileNotFound.new(@path) unless page_file_exists?

  Geb.log ""
  Geb.log " - loading page: #{page_name}"

  # read the page file, raise an error if the file could not be read
  begin
    @content = File.read(@path)
  rescue => e
    raise PageFileReadFailure.new(e.message)
  end # begin

  # parse the page content
  parse()

end

Instance Attribute Details

#contentString (readonly)

Returns the page content.

Returns:

  • (String)

    the page content



58
59
60
# File 'lib/geb/page.rb', line 58

def content
  @content
end

#parsed_contentString (readonly)

Returns the parsed page content.

Returns:

  • (String)

    the parsed page content



62
63
64
# File 'lib/geb/page.rb', line 62

def parsed_content
  @parsed_content
end

#pathString (readonly)

Returns the page path.

Returns:

  • (String)

    the page path



54
55
56
# File 'lib/geb/page.rb', line 54

def path
  @path
end

#siteGeb::Site (readonly)

Returns the site object.

Returns:



50
51
52
# File 'lib/geb/page.rb', line 50

def site
  @site
end

Instance Method Details

#build(output_path) ⇒ Object

build the page, save it to the output folder

Parameters:

  • output_path (String)

    the output path

Raises:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/geb/page.rb', line 208

def build(output_path)

  # build the page
  Geb.log " - building page: #{page_name}"

  # strip whitespace (spaces and newlines) at the beginning and at the end of the file
  @parsed_content = @parsed_content.strip

  # build the output filename
  output_filename = @path.gsub(@site.site_path, output_path)

  # attempt to create the output directory and file
  begin

    # make sure the output directory for the file exists
    FileUtils.mkdir_p(File.dirname(output_filename))

    # write the content to the output file
    File.write(output_filename, @parsed_content)

  rescue => e
    raise FailedToOutputPage.new(e.message)
  end # begin rescue

end

#parseObject

parse the page content for templates and partials



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/geb/page.rb', line 95

def parse

  # initalise the new page content
  @parsed_content = @content.dup

  # parse the content for templates, partials and site variables
  @parsed_content = parse_for_templates(@parsed_content)
  @parsed_content = parse_for_partials(@parsed_content)
  @parsed_content = parse_for_site_variables(@parsed_content)

end

#parse_for_partials(content = @parsed_content) ⇒ String

parse the content for partials. This method will keep looking for partials until all partials are found as partials can be embeded in other partials.

Parameters:

  • content (String) (defaults to: @parsed_content)

    the content to parse for partials. Default is the parsed page content.

Returns:

  • (String)

    the parsed content, with no partials to parse.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/geb/page.rb', line 154

def parse_for_partials(content = @parsed_content)

  # initialise a flag to keep looking for partials
  keep_looking_for_partials = true

  # keep looking for partials until we find them all (partials can be embeded in other partials)
  while keep_looking_for_partials

    # attempt to extract the partial paths
    found_partials, content = Geb::Partial.process_partials(@site.site_path, content)

    # check if we found any partials, if not we are done
    keep_looking_for_partials = (found_partials != 0)

  end # while keep_looking_for_partials

  # return the parsed content with partials handled
  return content

end

#parse_for_site_variables(content = @parsed_content) ⇒ String

Note:

site variables are defined in the site configuration file

Note:

site variables are matched using the VARIABLE_PATTERN

parse the content for site variables

Parameters:

  • content (String) (defaults to: @parsed_content)

    the content to parse for site variables. Default is the parsed page content.

Returns:

  • (String)

    the parsed content, with no site variables to parse.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/geb/page.rb', line 180

def parse_for_site_variables(content = @parsed_content)

  # initialize the return parsed content
  return_parsed_content = content.dup

  # get the site variables, this method automatically loads release vs local variables
  site_variables = @site.site_config.get_site_variables() || {}

  # set the special variables
  site_variables['page_relative_path']  = @path.gsub(@site.site_path, '')
  site_variables['page_title']          = content.match(PAGE_TITLE_PATTERN)[1] if content.match(PAGE_TITLE_PATTERN)
  site_variables['site_name']           = @site.site_config.site_name
  site_variables['geb_version']         = Geb::VERSION

  # find _{name}_ and replace with the site variable value
  return_parsed_content.gsub!(VARIABLE_PATTERN) do |match|
    key = match[2..-3] # remove _{ and }_
    site_variables[key] || match
  end # content.gsub!

  # return the parsed content with variables handled
  return return_parsed_content

end

#parse_for_templates(content = @parsed_content) ⇒ String

parse the content for templates. This method will keep looking for templates until all templates are found as templates can be based on templates.

Parameters:

  • content (String) (defaults to: @parsed_content)

    the content to parse for templates. Default is the parsed page content.

Returns:

  • (String)

    the parsed content, with no templates to parse.

Raises:



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
# File 'lib/geb/page.rb', line 112

def parse_for_templates(content = @parsed_content)

  # initialise a flag to keep looking for templates
  keep_looking_for_template = true

  # keep looking for template until we find them all
  while keep_looking_for_template

    # attempt to extract the template path and sections
    template_path     = Geb::Template.extract_template_path(content)
    template_sections = Geb::Template.extract_sections_for_template(content)

    # raise an error if template sections are defined but the template is not
    raise PageMissingTemplateDefition.new(page_name) if template_sections.any? && template_path.nil?

    # check if we found a template
    if template_path.nil?

      # we did not find a template, so we are done
      keep_looking_for_template = false

    else

      # create a new template object
      template = Geb::Template.load(File.join(@site.site_path, template_path))

      # parse the template content and replace the insert sections
      content = template.parse(template_sections)

    end # unless template_path.nil?

  end # while keep_looking_for_template

  # return the parsed content with templates handled
  return content

end