Class: Markwiki::Init

Inherits:
Jewel::Gem
  • Object
show all
Defined in:
lib/markwiki/init.rb

Overview

Wrapper class for Markwiki initialization

Class Method Summary collapse

Class Method Details

.default_configHash

Get a Hash representation of a default Markwiki configuration

Returns:

  • (Hash)

    the default Markwiki configuration



110
111
112
# File 'lib/markwiki/init.rb', line 110

def self.default_config
    self.generate_config
end

.generate_config(name: "markwiki", files: ["index.html"], css: "css", css_files: ["styles.css"], js: "js", js_files: ["scripts.js"], img: "img", img_files: []) ⇒ Hash

Get a Hash representation of an arbitrary Markwiki configuration

Parameters:

  • files (Array<String>) (defaults to: ["index.html"])

    an Array of top-level filenames

  • css (String) (defaults to: "css")

    the name of the CSS folder

  • css_files (Array<String>) (defaults to: ["styles.css"])

    an Array of CSS files

  • js (String) (defaults to: "js")

    the name of the JavaScript folder

  • js_files (Array<String>) (defaults to: ["scripts.js"])

    an Array of JavaScript files

  • img (String) (defaults to: "img")

    the name of the images folder

  • img_files (Array<String>) (defaults to: [])

    an Array of image files

Returns:

  • (Hash)

    a new Markwiki configuration Hash



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/markwiki/init.rb', line 125

def self.generate_config(
    name: "markwiki",
    files: ["index.html"],
    css: "css",
    css_files: ["styles.css"],
    js: "js",
    js_files: ["scripts.js"],
    img: "img",
    img_files: [])
    {
        "name" => name,
        "files".to_s => files + [".markwiki.cfg"],
        "css".to_s => {
            "dir_name" => css,
            "files" => css_files
        },
        "js".to_s => {
            "dir_name" => js,
            "files" => js_files
        },
        "img".to_s => {
            "dir_name" => img,
            "files" => img_files
        }
    }
end

.generate_json_configString

Generate a JSON String representation of a Markwiki site.

Returns:

  • (String)

    the Markwiki configuration as a JSON String



94
95
96
# File 'lib/markwiki/init.rb', line 94

def self.generate_json_config
    CONFIG.to_json
end

.generate_pretty_json_configString

Generate a prettified JSON String representation of a Markwiki site.

Returns:

  • (String)

    the Markwiki configuration as a JSON String



102
103
104
# File 'lib/markwiki/init.rb', line 102

def self.generate_pretty_json_config
    JSON.pretty_generate CONFIG
end

.generate_yaml_config(config) ⇒ String

Generate a YAML representation of a configuration

Parameters:

  • config (Hash)

    a Markwiki configuration

Returns:

  • (String)

    a YAML representation of the configuration



86
87
88
# File 'lib/markwiki/init.rb', line 86

def self.generate_yaml_config(config)
    YAML.dump(config)
end

.init_site(site_name, config: self.load_default_config) ⇒ Object

Creates the Markwiki directory structure from a configuration.

Parameters:

  • site_name (String)

    the name of Markwiki site

  • config (Hash) (defaults to: self.load_default_config)

    a Markwiki configuration Hash



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
79
80
# File 'lib/markwiki/init.rb', line 35

def self.init_site(site_name, config: self.load_default_config)
    Dir.mkdir(site_name)

    # Create the directories
    config.each_key do |key|
        if config[key].is_a? Hash
            Dir.mkdir(File.join(site_name, config[key]["dir_name"]))
        end
    end

    # Create the files
    config.each_key do |key|
        # We have top-level files
        if config[key].is_a? Array and key.eql? "files"
            config[key].each do |file|
                unless file.eql? ".markwiki.cfg"
                    #puts File.join(site_name, file)
                    FileUtils.touch(File.join(site_name, file))
                end
            end
        end

        # We have a subdirectory
        if config[key].is_a? Hash
            config[key].each_key do |subdiropt|
                if config[key][subdiropt].is_a? Array and subdiropt.eql? "files"
                    config[key][subdiropt].each do |file|
                        #puts File.join(site_name, config[key]["dir_name"], file)
                        FileUtils.touch(File.join(site_name, config[key]["dir_name"], file))
                    end
                end
            end
        end
    end

    # Create the Markwiki configuration
    if config.eql? self.load_default_config
        # Path to the default configuration
        path = self.root.static(".markwiki.cfg").to_s
        FileUtils.cp(path, "#{site_name}")
    else
        File.open("#{site_name}/.markwiki.cfg", "w") do |file| 
            file.write(self.generate_yaml_config(config))
        end
    end
end

.load_default_configHash

Load the default Markwiki configuration file

Returns:

  • (Hash)

    the default configuration as a Hash



155
156
157
158
159
160
161
162
# File 'lib/markwiki/init.rb', line 155

def self.load_default_config
    config = nil
    path = self.root.static(".markwiki.cfg").to_s
    file = File.open(path, "r") { |file| 
        config = YAML.load(file)
    }
    config
end