#!/usr/bin/env ruby
# frozen_string_literal: true

require 'json'
require 'icalendar'
require 'kramdown'
require 'kramdown-math-katex'


# Important!!!
# First ignore the node_modules, we do not need any of it on the site directly.
ignore '/node_modules/**/*'

#
#
# PREPROCESS
#
#


preprocess do
  `yarn`

  # We don't want to compile old blogposts in development
  ignore_old_content('blog', 'events', 'about/verslagen') if development?

  update_blog_attributes
  convert_tags('Blog')
  convert_tags('Events')
  create_yearly_items('Blog')
  create_yearly_items('Events')
  create_tagly_items('Blog')
  create_tagly_items('Events')
  convert_event_time_to_timestamps

  all_events.each do |event|
    check_schema(:event, event)
  end

  all_privacy_items.each do |project|
    check_schema(:privacy, project)
  end

  add_report_metadata
  add_project_metadata
  convert_locations
end

#
#
# COMPILATION
#
#
compile '/*.xml' do
  filter :erb
end

#
# ARCHIVES
#
compile '/blog/*' do
  layout '/blogs.*'
  layout '/generic.*'
  layout '/default.*'
  filter :erb

  filter :html_press if production?
end

#
# EVENTS
#

# Overview page
compile '/events/*' do
  layout '/events.*'
  layout '/generic.*'
  layout '/default.*'
  filter :erb

  filter :html_press if production?
end

compile '/events/*/*' do
  filter :erb
  filter :kramdown, {math_engine: :katex}

  layout '/eventpost.*'
  layout '/default.*'
  filter :erb

  filter :html_press if production?
end

compile '/events/*/*', rep: :text do
  filter :kramdown
  filter :strip_html
end

compile '/events/*/*', rep: :ical do
  filter :ical
  write ext: 'ics'
end

#
# POSTS
#
compile '/blog/*/*.md' do
  filter :erb
  layout '/blogpost.md'
  filter :kramdown, {math_engine: :katex}
  filter :typogruby_custom

  layout '/blogpost.erb'
  layout '/generic.*'
  layout '/default.*'
  filter :erb

  filter :html_press if production?
end

compile '/blog/*/*.md', rep: :text do
  filter :erb
  filter :kramdown
  filter :strip_html
end

compile '/blog/*/*.md', rep: :html do
  filter :erb
  filter :kramdown
end

#
# PROJECTS
#
compile '/projects/*' do
  filter :kramdown

  # Don't write out the projects themselves for now
  write nil
end

compile '/*_search.json' do
  filter :erb
end

compile '/**/*.ics' do
  filter :erb
end

#
# PRIVACY
#

compile '/about/privacy/*' do
  filter :kramdown

  # Don't write out the privacy items themselves for now
  nil
end

#
# REPORTS
#

# Note drive/verslagen is 'linked' as a data source to /about/verslagen/
compile '/about/verslagen/*/*.md', rep: :pdf do
  filter :pandoc_pdf, args: { f: :markdown, 'pdf-engine': 'xelatex', template: 'templates/report.tex' } if production?
  write ext: (production? ? 'pdf' : 'md')
end

#
# GENERIC ERB PAGES
#
compile '/**/*.erb' do
  filter :erb

  # Apply typographic improvements if required by the page. Use this on text-heavy pages,
  # such as the history page.
  if @item[:typography]
    filter :typogruby_custom
  end

  layout '/generic.*'
  layout '/default.*'

  filter :html_press if production?
end

compile '/**/*.md' do
  filter :erb
  filter :kramdown

  layout '/generic.*'
  layout '/default.*'
end

#
# ASSETS
#
compile '/assets/scripts/**/*.js' do
  filter :uglify_js, harmony: true if production?
end

ignore '/data/**/*'

ignore '/assets/stylesheets/includes/**/*'
compile '/assets/stylesheets/**/*.scss' do
  # This filter is necessary for the workaround present in main.scss and deals with out of date dependencies
  filter :erb

  sass_opts = {
    syntax: :scss,
    load_paths: ['content/assets/stylesheets']
  }
  sass_opts[:style] = :compressed if production?
  filter :sassc, sass_opts

  filter :autoprefixer if production?
  write ext: 'css'
end

compile '/assets/stylesheets/**/*.css' do
  if production?
    filter :rainpress
    filter :autoprefixer
  end
  write ext: 'css'
end

#
#
# ROUTES
#
#

# Google verification file
passthrough '/google6f2e77d0228abc35.html'

route '/**/index.{erb,html,md}' do
  "#{item.identifier.without_ext}.html"
end

route '/**/*.{erb,html,md}' do
  "#{item.identifier.without_ext}/index.html"
end

route %r[/well-known/(.+)] do |rest|
  "/.well-known/" + rest[0]
end

# Let anything else simply pass through
passthrough '/**/*'

#
#
# LAYOUTS
#
#

layout '/**/*', :erb
