Module: Sawsge

Defined in:
lib/blog.rb,
lib/home.rb,
lib/page.rb,
lib/post.rb,
lib/sawsge.rb,
lib/project.rb,
lib/resource.rb,
lib/sawsge/version.rb

Defined Under Namespace

Classes: Home, Page, Post, Resource

Constant Summary collapse

HELP_STRING =
'Usage: sawsge [DIRECTORY]'
SRC_DIR =
ARGV[0] || Dir.pwd
CONFIG_FILENAME =
'config.toml'
CONFIG_STRING =
File.read(File.join(SRC_DIR, CONFIG_FILENAME))
CONFIG =
TOML::Parser.new(CONFIG_STRING).parsed
OUT_DIRNAME =
CONFIG['general']['out_dirname']
POSTS_DIRNAME =

TODO: Put these in the config

CONFIG['blog']['posts_dirname']
HEADER_FILENAME =
CONFIG['general']['header_filename']
CONFIG['general']['footer_filename']
HEADER =
HEADER_FILENAME.empty? ? '' : File.read(File.join(SRC_DIR, HEADER_FILENAME))
FOOTER_FILENAME.empty? ? '' : File.read(File.join(SRC_DIR, FOOTER_FILENAME))
CONFIG['general']['external_links_target_blank']
IGNORE =
CONFIG['general']['ignore']
RESERVED_FILENAMES =

Resources that will not be put into the out folder

[CONFIG_FILENAME, HEADER_FILENAME, FOOTER_FILENAME] + IGNORE
MODE =
CONFIG['general']
VERSION =
'0.1.3'

Class Method Summary collapse

Class Method Details

.blogObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/blog.rb', line 4

def self.blog
  home_path = 'index.md'

  # Does not work if you have parent directories for your
  # posts dir, e.g. you set posts_dirname in config.toml to
  # foo/bar/baz/etc...
  post_paths = resource_paths.select do |path|
    top_parent_dir(path) == POSTS_DIRNAME && File.extname(path) == '.md'
  end
  # So posts are added to Home in chronological order
  post_paths.reverse!

  @resource_paths.subtract post_paths
  @resource_paths.delete home_path

  post_objects = post_paths.map { |path| Post.new(path) }
  home_object = Home.new(home_path, post_objects)
  @all_objects = post_objects + [home_object]
end

.projectObject



4
5
6
7
8
9
10
11
12
# File 'lib/project.rb', line 4

def self.project
  page_paths = @resource_paths.select { |path| File.extname(path) == '.md' }

  @resource_paths.subtract page_paths

  page_objects = page_paths.map { |path| Page.new(path) }

  @all_objects.merge page_objects
end

.sawsgeObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sawsge.rb', line 53

def self.sawsge
  # Gross, but easy
  Dir.chdir SRC_DIR

  # Find all files recursively
  @resource_paths = Set.new(Dir.glob('**/*').select do |path|
    File.file?(path) &&
      top_parent_dir(path) != OUT_DIRNAME &&
      !RESERVED_FILENAMES.include?(path)
  end)

  @resource_objects = Set.new
  @all_objects = Set.new

  send MODE

  resources = @resource_paths.map { |path| Resource.new(path) }
  @all_objects.merge resources

  # Delete any old builds
  FileUtils.remove_dir OUT_DIRNAME if Pathname.new(OUT_DIRNAME).exist?
  FileUtils.mkpath OUT_DIRNAME

  # Write each file
  @all_objects.each(&:build)
end

.top_parent_dir(path) ⇒ Object

Returns the first directory in a path, eg. ‘foo/bar/bin.txt` becomes `foo`



49
50
51
# File 'lib/sawsge.rb', line 49

def self.top_parent_dir(path)
  Pathname.new(path).each_filename.to_a[0]
end