Perron Static Site Generator
repository·main·Indexed 41 days ago
https://github.com/rails-designer/perronA Static Site Generator (SSG) for Ruby on Rails that enables developers to build static websites using Rails-based workflows. It provides tools for managing content collections, processing structured data from YAML, JSON, and CSV files, and rendering Markdown into HTML.
What's inside Perron
- Perron is a Static Site Generator (SSG) designed for Ruby on Rails. It allows developers to leverage the Rails ecosystem to build and deploy static websites.
Consume structured data with Perron
mainPerron allows you to consume structured data from
.yml,.json, or.csvfiles and make them available within your Rails views. This is ideal for populating repeated data structures like team members, features, or site content.By default, Perron looks for data files in
app/content/data/. The class name used to access the data in your application matches the file's basename (e.g.,features.ymlis accessed viaContent::Data::Features).<% Content::Data::Features.all.each do |feature| %> <h4><%= feature.name %></h4> <p><%= feature.description %></p> <% end %>Define data file structures
mainData resources must be formatted as an array of objects. If you intend to use the
.findmethod or define associations, every record must include anidfield.# app/content/data/authors.yml - id: rails-designer name: Rails Designer bio: Creator of Perron - id: cam name: Cam bio: Contributing authorConfigure Perron via the configuration block
mainYou can customize Perron's behavior by using the
Perron.configuremethod. This method yields the current configuration object, allowing you to set various options such as output paths, build modes, and live reload settings.Example usage:
Perron.configure do |config| config.output = "custom_output" config.mode = :standalone config.site_name = "My Awesome Site" endPerron.configure do |config| config.output = "custom_output" config.mode = :standalone config.site_name = "My Awesome Site" endRender data collections using partials
mainYou can render entire data collections directly using Rails-style partial rendering.
When you call
<%= render Content::Data::Features.all %>, Perron expects a partial located atapp/views/content/features/_feature.html.erb. The partial will be rendered once for each item, and the individual record is passed as a local variable named after the singular form of the collection (e.g.,feature).<%# In your view %> <%= render Content::Data::Features.all %> <%# app/views/content/features/_feature.html.erb %> <div class="feature"> <h4><%= feature.name %></h4> <p><%= feature.description %></p> </div>Configure data file locations
mainPerron automatically searches
app/content/data/for files with.yml,.json, or.csvextensions.If you need to access a data resource located at a custom path within
/app/content/data/, you can instantiate a new data object usingContent::Data.new("path/to/data-resource").Configure the Markdown parser and options
mainYou can control which Markdown engine Perron uses and how it is configured via
Perron.configuration.Parser Selection
Set
Perron.configuration.markdown_parserto the name of the parser class you wish to use. Perron will attempt to find the class by camelizing the name and appendingParserif necessary (e.g., setting it to:redcarpetlooks forRedcarpetParser).Parser Options
Pass a hash of options to
Perron.configuration.markdown_options. These options are passed directly to the parser's initializer.Supported Parsers
RedcarpetParser: Requires theredcarpetgem. Supports:renderer_optionsand:markdown_optionswithin the main options hash.KramdownParser: Requires thekramdowngem.CommonMarkerParser: Requires thecommonmarkergem.
# Example configuration Perron.configure do |config| config.markdown_parser = :redcarpet config.markdown_options = { markdown_options: { hard_wrap: true }, renderer_options: { filter_html: true } } endConfigure title tag suffix and separator
mainThe
Perron::Metatags#title_tagmethod uses global configuration to format the<title>element.If
Perron.configuration.metadata.title_suffixis set, it will be appended to the resource title using thePerron.configuration.metadata.title_separator. The suffix is only appended if the resource title is not identical to the suffix itself.Troubleshoot Perron::DataSource parsing errors
mainWhen working with data files in Perron, you may encounter several specific error types if the data format is invalid or inconsistent:
Errors
Perron::Errors::DataParseError: Raised when:- The top-level data structure is not an
Array. - An item in the array is neither a
Hashnor aString. - There is a syntax error in YAML, JSON, or CSV.
- ERB rendering fails.
- In CSV files, a row has missing or extra columns compared to the header row.
- The top-level data structure is not an
Perron::Errors::UnsupportedDataFormatError: Raised when attempting to use a file extension that is not.yml,.yaml,.json, or.csv.
Common Scenarios
- CSV Column Mismatch: If a CSV row does not match the headers of the first row, a
DataParseErroris raised detailing which columns are missing or extra. - Invalid JSON/YAML: The error message will typically include line and column information to help locate the syntax error.
Render resource content with a layout
mainThe
inlinemethod allows you to render the resource's content within a specific layout, returning a hash containing the HTML and the layout name. This is useful for generating page fragments or full views.resource = Perron::Resource.new("page.md") render_data = resource.inline(layout: "blog_post") # => { html: "<html>...</html>", layout: "blog_post" }Access resource content and metadata
mainOnce initialized, you can access the resource's data using several methods:
content: Returns the processed content. If the file is an.erbfile or if metadata specifieserb: true, it will process ERB templates.raw(orraw_content): Returns the unprocessed string content of the file.metadata: Returns a hash of data parsed from the file's frontmatter.slug(orpath, orto_param): Returns the URL-friendly slug generated from the resource.
resource = Perron::Resource.new("page.md") resource.content # Processed content (with ERB if applicable) resource.raw # The raw file string resource.metadata # Hash of frontmatter data resource.slug # The URL slugRender SEO metatags with Perron::Metatags
mainThe
Perron::Metatagsclass converts a data hash into HTML SEO tags (title, canonical, Open Graph, and Twitter cards).To use it, initialize the class with a hash containing your resource data and call
render. You can filter which tags are rendered using theonlyorexceptoptions.Supported Data Keys
Key HTML Tag Type :title<title>:canonical_url<link rel="canonical">:description<meta name="description">:article_published_time<meta property="article:published_time">:og_title<meta property="og:title">:og_type<meta property="og:type">:og_url<meta property="og:url">:og_image<meta property="og:image">:og_description<meta property="og:description">:og_site_name<meta property="og:site_name">:og_logo<meta property="og:logo">:og_author<meta property="og:author">:og_locale<meta property="og:locale">:twitter_card<meta name="twitter:card">:twitter_title<meta name="twitter:title">:twitter_description<meta name="twitter:description">:twitter_image<meta name="twitter:image"># Example usage in a Rails view or controller data = { title: "My Page", description: "A great description", og_title: "My Page Title", og_type: "website" } metatags = Perron::Metatags.new(data) # Render all available tags metatags.render # Render only specific tags metatags.render(only: [:title, :description]) # Exclude specific tags metatags.render(except: [:og_title, :og_type])