Style folder
It’s possible to write your own routines which will be used by Fragmenta when rendering templates and fragments. Some examples are shown below, taken from the actual files used by Fragmenta.
Controllers
Controllers allow you to add code to the Fragmenta application before it generates the website. You can modify any of the built in routines in the Page or Fragment objects, and thus modify the behaviour of the application.
At it’s simplest, these modifications can consist of shortcut routines like the following one added to page.rb (used to generate google links in these very help files) :
# Shortcut for google link
#
#
def g(text)
link(:google => text)
end
You could also add routines to parse the file system of the build machine, pull files off the internet, format dates and strings, or anything else you can do with normal ruby scripts. These files are located in the controllers directory under the ‘Style’ directory.
Models
Models allow you to add code to change the way that the application loads certain file types. If you need some examples you can look inside the application itself, where 4 default models are used to parse various types of file.
Each model has its own file type(s), defined by file extensions, and is used to parse any fragment files having those extensions. There are three required routines which must be present in each model class.
- file_readable(in_path)
- parse_file(in_fragment,in_path)
- output_files(in_fragment,output_dir)
So for example if we were to write a model to parse ruby files, it might look something like this :
#!/usr/bin/ruby
# parses ruby code files for fragment
require 'field_helper'
require 'singleton'
class FragmentRubycode
include Singleton
attr_accessor :type
# set up a class variable
#
#
def initialize
@type = 'text'
@file_types = ['.rb']
end
# Can we read this file?
#
#
def file_readable?(in_path)
@file_types.include?(File.extname(in_path))
end
# Read content from the file path given
#
#
def parse_file(in_fragment,in_path)
# reads our fields into our @fields from file
extra_fields = read_text_file(in_path,in_fragment)
# set the fragment fields
in_fragment.add_fields(extra_fields)
end
# Output files - as a text object we do nothing
#
#
def output_files(in_fragment,output_dir)
end
# Read in the fields and text from a text file
#
# Each fragment can have zero or more fields and the rest will be treated as a text field
def read_text_file(in_path,in_fragment)
file_fields = Hash.new()
# set the view to our custom ruby view
file_fields["view"] = "fragment/ruby"
file_fields["text"] = "<code><pre>File.read(in_path)</pre></code>"
file_fields
end
Fragmenta manual
