Class: Frozen::Generators::MdGenerator

Inherits:
FrozenRails::Generator show all
Defined in:
lib/generators/frozen/md_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_configObject



36
37
38
39
40
41
42
# File 'lib/generators/frozen/md_generator.rb', line 36

def add_config
  append_to_application_config <<~RUBY
    # frozen:md
    config.assets.paths << Rails.root.join("content")
    config.action_dispatch.rescue_responses["Decant::FileNotFound"] = :not_found
  RUBY
end

#add_gemsObject



13
14
15
16
17
18
19
20
21
# File 'lib/generators/frozen/md_generator.rb', line 13

def add_gems
  add_frozen_gems <<~RUBY
    # frozen:md
    gem "decant"
    gem "kramdown"
    gem "kramdown-parser-gfm"
    gem "rouge"
  RUBY
end

#add_routesObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/frozen/md_generator.rb', line 44

def add_routes
  append_to_routes <<~RUBY
    root "categories#index"

    # frozen:md
    constraints slug: Regexp.union(Category.all.map(&:slug)) do
      resources :categories, param: :slug, only: [ :index, :show ]
    end
    resources :pages, param: :slug, only: [ :show ]
  RUBY
end

#bundle_gemsObject



23
24
25
# File 'lib/generators/frozen/md_generator.rb', line 23

def bundle_gems
  bundle!
end

#copy_filesObject



27
28
29
30
31
32
33
34
# File 'lib/generators/frozen/md_generator.rb', line 27

def copy_files
  copy_directory "controllers", "app/controllers"
  copy_directory "helpers", "app/helpers"
  copy_directory "models", "app/models"
  copy_directory "views", "app/views"
  copy_directory "initializers", "config/initializers"
  copy_directory "pages", "content/pages"
end

#generate_rouge_css_stylesheetObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/generators/frozen/md_generator.rb', line 61

def generate_rouge_css_stylesheet
  theme = options[:rouge_theme]

  if theme.blank? && behavior == :invoke && $stdin.tty?
    # Run in subshell so we pick up newly installed gem without needing to require it in the current process.
    themes = `bundle exec ruby -e "require 'rouge'; puts Rouge::Theme.registry.keys.sort"`.split("\n")

    say_status :info, "Available Rouge themes (#{themes.size}):", :blue
    themes.each_with_index { |t, i| say "  #{i + 1}. #{t}" }

    answer = ask("Choose a theme (name or number) [github]")

    theme = if /^\d+$/.match?(answer)
      themes[answer.to_i - 1]
    else
      answer.presence
    end
  end

  theme ||= "github"

  say_status :info, "Generating Rouge CSS for theme #{theme}", :blue
  run "rougify style #{theme} > app/assets/rouge/rouge.css"
end

#prepare_rougeObject



56
57
58
59
# File 'lib/generators/frozen/md_generator.rb', line 56

def prepare_rouge
  copy_directory "assets/rouge", "app/assets/rouge"
  copy_file "assets/application.css", "app/assets/application.css", force: true
end