module Schizm::Env

Public Class Methods

author() click to toggle source

Current project author.

# File lib/schizm/env.rb, line 77
def self.author
  return var[:author] if var? :author
  return thisuser
end
blaze(path) click to toggle source

Create missing directories in path.

# File lib/schizm/env.rb, line 142
def self.blaze path
  dir = ''
  path = File.dirname path
  path = File.absolute_path path
  dirs = path.split File::SEPARATOR
  dirs.each do |sub|
    dir = File.join dir, sub
    unless Dir.exist? dir
      puts "Making '#{dir}'" if var? :verbose
      Dir.mkdir dir
    end
  end
end
brief() click to toggle source

Current project brief.

# File lib/schizm/env.rb, line 71
def self.brief
  return var[:brief] if var? :brief
  return ""
end
build() click to toggle source
# File lib/schizm/env.rb, line 447
  def self.build
    zrc_root = var[:input]
    doc_root = var[:output_doc]
    src_root = var[:output_src]
    zrc_globs = []
    zrc_globs.push File.join(zrc_root, "*.zm")
    zrc_globs.push File.join(zrc_root, "*/*.zm")
    zrc_paths = Dir.glob zrc_globs
    raise "No input files." if zrc_paths.empty?
    delete doc_root if var? :clean_doc
    delete src_root if var? :clean_src
    blaze doc_root
    blaze src_root
    zrc_paths.each do |path|
      filename = path_from zrc_root, path
      filename = File.join doc_root, filename.chomp(".zm").concat(".html")
      input = File.read path
      Parse.hash[filename] =
      Parse.page_elems filename, input
    end
    unless Env.var? :only_src
      Parse.hash.each do |filename, parse|
        blaze filename
        write filename, <<PARSE
---
layout: schizm
author: "#{author}"
#{parse.guess_title}
#{parse.guess_description}
#{parse.guess_parts}
---
#{parse.to_s}
PARSE
      end
    end
    unless Env.var? :only_doc
      Chunk.each do |chunk|
        if chunk.target?
          blaze filename = File.join(src_root, chunk.target)
          write filename, chunk
        end
      end
    end
  end
delete(path) click to toggle source

Delete path if it exists. If path is a directory, recursively delete all subdirectories.

# File lib/schizm/env.rb, line 126
def self.delete path
  if File.exist? path
    puts "Deleting #{path}" if var? :verbose
    if File.file? path
      File.delete path
    elsif File.directory? path
      Dir.foreach path do |subpath|
        next if subpath == '.'
        next if subpath == '..'
        self.delete File.join(path, subpath)
      end
    end
  end
end
docs_path(path) click to toggle source
# File lib/schizm/env.rb, line 161
def self.docs_path path
  return "/docs/#{path_from var[:output_doc], path}"
end
home() click to toggle source

Schizm install directory.

# File lib/schizm/env.rb, line 89
def self.home
  path = File.dirname __FILE__    # home/lib/schizm
  path = File.dirname path        # home/lib
  path = File.dirname path        # home
  return File.absolute_path path
end
init() click to toggle source
# File lib/schizm/env.rb, line 257
  def self.init
    dirnames = [
      "src",
      "zrc",
      "docs",
      "docs/assets",
      "docs/assets/css",
      "docs/assets/fonts",
      "docs/assets/js",
      "docs/_includes",
      "docs/_layouts",
      "docs/_plugins",
      "docs/_posts",
      "docs/_docs",
      "docs/_site"
    ]
    dirnames.each do |dirname|
      unless File.directory? dirname
        Dir.mkdir dirname
      end
    end
    license = String.new
    license_content = String.new
    if var? :license
      license << var[:license]
      license_content << "Copyright (c) #{year} #{author}\n\n"
      license_content << "#{resource("license", license)}"
      write "LICENSE", license_content
    end
    write "README.md", <<README
# #{title}
README
    write "Makefile", <<MAKEFILE
SHELL := /bin/bash
MKDIR := mkdir -p
RMDIR := rm -r -f

SCHIZM := schizm
ZFLAGS := --input zrc
ZFLAGS += --output-src src
ZFLAGS += --output-doc docs/_docs
ZFLAGS += --rewrite
ZFLAGS += --verbose
ZFLAGS += --title "#{title}"
ZFLAGS += --brief "#{brief}"
ZFLAGS += --author "#{author}"
ZFLAGS += --year "#{year}"

doc:
\t@$(SCHIZM) build --only-doc $(ZFLAGS)

src:
\t@$(SCHIZM) build --only-src $(ZFLAGS)

.PHONY: doc

.PHONY: src

clean:
\t@$(RMDIR) src docs/_docs docs/_site
\t@$(MKDIR) src docs/_docs docs/_site

.PHONY: clean
MAKEFILE
    write ".gitignore", <<GITIGNORE
*.swp
*.tmp
docs/_site
GITIGNORE
    write "docs/_config.yml", <<YMLCONFIG
source: .
destination: _site
plugins_dir: _plugins
layouts_dir: _layouts
includes_dir: _includes
collections:
  posts:
    output: true
  docs:
    output: true
encoding: "utf-8"
markdown: kramdown
markdown_ext: "markdown,md"
author: "#{author}"
title: "#{title}"
brief: "#{brief}"
YMLCONFIG
    assets = {
      "schizm.liquid" => "docs/_layouts",
      "schizm.js" => "docs/assets/js",
      "fonts/Fira-Code.css" => "docs/assets/fonts",
      "fonts/Fira-Code-Light.otf" => "docs/assets/fonts",
      "fonts/Fira-Code-Regular.otf" => "docs/assets/fonts",
      "fonts/Fira-Code-Medium.otf" => "docs/assets/fonts",
      "fonts/Fira-Code-Bold.otf" => "docs/assets/fonts",
      "fonts/Fira-Code-LICENSE" => "docs/assets/fonts",
      "fonts/Genericons-Neue.css" => "docs/assets/fonts",
      "fonts/Genericons-Neue.eot" => "docs/assets/fonts",
      "fonts/Genericons-Neue.ttf" => "docs/assets/fonts",
      "fonts/Genericons-Neue.woff2" => "docs/assets/fonts",
      "fonts/Genericons-Neue-LICENSE" => "docs/assets/fonts",
      "fonts/Social-Logos.css" => "docs/assets/fonts",
      "fonts/Social-Logos.eot" => "docs/assets/fonts",
      "fonts/Social-Logos.ttf" => "docs/assets/fonts",
      "fonts/Social-Logos.woff2" => "docs/assets/fonts",
      "fonts/Social-Logos-LICENSE" => "docs/assets/fonts"
    }
    assets.each do |from, to|
      write File.join(to, File.basename(from)), resource(from)
    end
    color1 = "purple"
    color2 = "teal"
    color1 = var[:primary_color].downcase if var? :primary_color
    color2 = var[:secondary_color].downcase if var? :secondary_color
    sass_source = <<SASS
#{Schizm.sass_colors color1, "primary"}
#{Schizm.sass_colors color2, "secondary"}
#{resource "scss/fonts.scss"}
#{resource "scss/mixin.scss"}
#{resource "scss/style.scss"}
SASS
    sass_options = {
      :syntax => :scss,
      :sourcemap => :none,
      :style => :nested,
      :cache => false
    }
    write "docs/assets/css/schizm.css", Sass::Engine.new(sass_source, sass_options).render
  end
path_from(from, dest) click to toggle source

Return path dest relative to from.

# File lib/schizm/env.rb, line 157
def self.path_from from, dest
  return Pathname.new(dest).relative_path_from(Pathname.new(from)).to_s
end
resource(*args) click to toggle source

Load resource from Schizm install directory.

# File lib/schizm/env.rb, line 97
def self.resource *args
  return File.read File.join(home, "res", *args)
end
thisdirname() click to toggle source

Current user's working directory.

# File lib/schizm/env.rb, line 60
def self.thisdirname
  return File.basename(Dir.pwd)
end
thisuser() click to toggle source

Current user's name.

# File lib/schizm/env.rb, line 55
def self.thisuser
  return Etc.getpwnam(Etc.getlogin).gecos.split(/,/).first
end
title() click to toggle source

Current project title.

# File lib/schizm/env.rb, line 65
def self.title
  return var[:title] if var? :title
  return thisdirname
end
var() click to toggle source

Global variable hash getter.

# File lib/schizm/env.rb, line 43
def self.var
  return @@var
end
var?(key) click to toggle source

true if +@@var+ is neither nil nor false.

# File lib/schizm/env.rb, line 48
def self.var? key
  return true if @@var.has_key?(key) and
    @@var[key] != nil and @@var[key] != false
  return false
end
vim_install() click to toggle source
# File lib/schizm/env.rb, line 523
def self.vim_install
  vimhome = "~/.vim"
  vimhome = var[:vimhome] if var? :vimhome
  vimhome = File.absolute_path File.expand_path(vimhome)
  assets = {
    "vim/ftdetect/schizm.vim" => File.join(vimhome, "ftdetect"),
    "vim/ftplugin/schizm.vim" => File.join(vimhome, "ftplugin"),
    "vim/indent/schizm.vim" => File.join(vimhome, "indent"),
    "vim/syntax/schizm.vim" => File.join(vimhome, "syntax")
  }
  assets.each do |from, to|
    blaze File.join(to, File.basename(from))
    write File.join(to, File.basename(from)), resource(from)
  end
end
vim_uninstall() click to toggle source
# File lib/schizm/env.rb, line 539
def self.vim_uninstall
  vimhome = "~/.vim"
  vimhome = var[:vimhome] if var? :vimhome
  vimhome = File.absolute_path File.expand_path(vimhome)
  assets = [
    File.join(vimhome, "ftdetect/schizm.vim"),
    File.join(vimhome, "ftplugin/schizm.vim"),
    File.join(vimhome, "indent/schizm.vim"),
    File.join(vimhome, "syntax/schizm.vim")
  ]
  assets.each do |asset|
    delete asset
  end
end
write(target, string) click to toggle source

Write string to target. If target is an existing file and var[:rewrite] is not set, prompts the user for permission to rewrite.

# File lib/schizm/env.rb, line 113
def self.write target, string
  unless File.file? target and 
      not var? :rewrite and 
      not yes? "Rewrite '#{target}'?", "n"
    puts "Writing '#{target}'" if var? :verbose
    file = File.open target, "wb"
    file.write string.to_s
    file.close
  end
end
year() click to toggle source

Current project year.

# File lib/schizm/env.rb, line 83
def self.year
  return var[:year] if var? :year
  return Time.new.year.to_s
end
yes?(string, backup) click to toggle source

Prompt the user with a yes/no question.

# File lib/schizm/env.rb, line 102
def self.yes? string, backup
  backup = backup.downcase[0]
  print "#{string} [Y/n] " if backup == "y"
  print "#{string} [y/N] " if backup == "n"
  answer = gets.strip.downcase[0]
  return answer == "y" if answer != ""
  return backup == "y"
end