Perron Static Site Generator

repository·main·Indexed 41 days ago

https://github.com/rails-designer/perron

A 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.

Tokens
6.4K
Snippets
24
Records
31
Agent score
100%

What's inside Perron

  1. Consume structured data with Perron

    main

    Perron allows you to consume structured data from .yml, .json, or .csv files 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.yml is accessed via Content::Data::Features).

    <% Content::Data::Features.all.each do |feature| %>
      <h4><%= feature.name %></h4>
      <p><%= feature.description %></p>
    <% end %>
  2. Define data file structures

    main

    Data resources must be formatted as an array of objects. If you intend to use the .find method or define associations, every record must include an id field.

    # app/content/data/authors.yml
    - id: rails-designer
      name: Rails Designer
      bio: Creator of Perron
    
    - id: cam
      name: Cam
      bio: Contributing author
  3. Configure Perron via the configuration block

    main

    You can customize Perron's behavior by using the Perron.configure method. 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"
    end
    Perron.configure do |config|
      config.output = "custom_output"
      config.mode = :standalone
      config.site_name = "My Awesome Site"
    end
  4. Render data collections using partials

    main

    You can render entire data collections directly using Rails-style partial rendering.

    When you call <%= render Content::Data::Features.all %>, Perron expects a partial located at app/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>
  5. Configure the Markdown parser and options

    main

    You can control which Markdown engine Perron uses and how it is configured via Perron.configuration.

    Parser Selection

    Set Perron.configuration.markdown_parser to the name of the parser class you wish to use. Perron will attempt to find the class by camelizing the name and appending Parser if necessary (e.g., setting it to :redcarpet looks for RedcarpetParser).

    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 the redcarpet gem. Supports :renderer_options and :markdown_options within the main options hash.
    • KramdownParser: Requires the kramdown gem.
    • CommonMarkerParser: Requires the commonmarker gem.
    # Example configuration
    Perron.configure do |config|
      config.markdown_parser = :redcarpet
      config.markdown_options = {
        markdown_options: { hard_wrap: true },
        renderer_options: { filter_html: true }
      }
    end
  6. Configure title tag suffix and separator

    main

    The Perron::Metatags#title_tag method uses global configuration to format the <title> element.

    If Perron.configuration.metadata.title_suffix is set, it will be appended to the resource title using the Perron.configuration.metadata.title_separator. The suffix is only appended if the resource title is not identical to the suffix itself.

  7. Troubleshoot Perron::DataSource parsing errors

    main

    When 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 Hash nor a String.
      • 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.
    • 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 DataParseError is 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.
  8. Render resource content with a layout

    main

    The inline method 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" }
  9. Access resource content and metadata

    main

    Once initialized, you can access the resource's data using several methods:

    • content: Returns the processed content. If the file is an .erb file or if metadata specifies erb: true, it will process ERB templates.
    • raw (or raw_content): Returns the unprocessed string content of the file.
    • metadata: Returns a hash of data parsed from the file's frontmatter.
    • slug (or path, or to_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 slug
  10. Render SEO metatags with Perron::Metatags

    main

    The Perron::Metatags class 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 the only or except options.

    Supported Data Keys

    KeyHTML 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])