Class: MarkdownSite::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_site/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, type) ⇒ Site

Returns a new instance of Site.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/markdown_site/site.rb', line 10

def initialize(config, type)
    @references = {}
    @reverse_references = {}
    @nodes = []
    @links = []
    @config = MarkdownSite::Config.new(config, type)            
    @languages = @config.languages
    if @config.type == :logseq
        @pages_path = @config.pages
    else
        @pages_path = @config.src
    end
    init_citations(type)
    if type == :wiki or type == :logseq or type == :obsidian
        init_pages
    end
    if type == :blog or type == :logseq
        init_journals
    end
    if type == :wiki or type == :obsidian
        init_summarys
    end
    gen_nodes_links
end

Instance Attribute Details

#citationsObject

Returns the value of attribute citations.



8
9
10
# File 'lib/markdown_site/site.rb', line 8

def citations
  @citations
end

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/markdown_site/site.rb', line 7

def config
  @config
end

#journalsObject

Returns the value of attribute journals.



7
8
9
# File 'lib/markdown_site/site.rb', line 7

def journals
  @journals
end

#languagesObject

Returns the value of attribute languages.



8
9
10
# File 'lib/markdown_site/site.rb', line 8

def languages
  @languages
end

Returns the value of attribute links.



8
9
10
# File 'lib/markdown_site/site.rb', line 8

def links
  @links
end

#nodesObject

Returns the value of attribute nodes.



8
9
10
# File 'lib/markdown_site/site.rb', line 8

def nodes
  @nodes
end

#pages(lang = nil) ⇒ Object

Returns the value of attribute pages.



7
8
9
# File 'lib/markdown_site/site.rb', line 7

def pages
  @pages
end

#pages_pathObject

Returns the value of attribute pages_path.



7
8
9
# File 'lib/markdown_site/site.rb', line 7

def pages_path
  @pages_path
end

#referencesObject

Returns the value of attribute references.



7
8
9
# File 'lib/markdown_site/site.rb', line 7

def references
  @references
end

#reverse_referencesObject

Returns the value of attribute reverse_references.



7
8
9
# File 'lib/markdown_site/site.rb', line 7

def reverse_references
  @reverse_references
end

Instance Method Details

#copy_assetsObject



210
211
212
213
214
215
216
217
218
# File 'lib/markdown_site/site.rb', line 210

def copy_assets
    init_publish_dir
    if Dir.exist?(@config.assets_dir)
        `cp -r #{@config.assets_dir} #{@config.publish_dir}/#{@config.assets_dir}`
        @config.copy_files.each do |file|
            `cp #{file} #{@config.publish_dir}/#{file}` 
        end
    end
end

#folder_file_comparison(a, b) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/markdown_site/site.rb', line 78

def folder_file_comparison(a,b)
    a_array = a.split("/")
    b_array = b.split("/")
    if a_array.length == 1
        if b_array.length == 1
            return a<=>b
        else
            return 1
        end
    else
        if a_array[0]==b_array[0]
            return folder_file_comparison(a_array[1..-1].join("/"),b_array[1..-1].join("/"))
        else
            return a_array[0]<=>b_array[0]
        end
    end
end


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/markdown_site/site.rb', line 160

def gen_nodes_links
    @references.each do |k,v|
        val = @references[k] ? @references[k].size+1 : 1
        val = 5 if val > 5
        @nodes << {
            "id" => k,
            "name" => k,
            "color" => "blue",
            "val" => val
        }
        v.each do |item|
            val = @references[item] ? @references[item].size+1 : 1
            val = 5 if val > 5
            @nodes << {
                "id" => item,
                "name" => item,    
                "color" => "blue",
                "val" => val
            }
            @links << {
                "source" => item,
                "target" => k
            }
        end
    end
    @nodes = @nodes.uniq
    @links = @links.uniq
end

#gen_references(item_name, text) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/markdown_site/site.rb', line 144

def gen_references(item_name, text)
    text.gsub(/\[\[([^\]]+)\]\]/) do |s|
        s = s[2..-3]
        if @references[s]
            @references[s] << item_name
        else
            @references[s] = [item_name]
        end
        if @reverse_references[item_name]
            @reverse_references[item_name] << s
        else
            @reverse_references[item_name] = [s]
        end
    end
end

#generateObject



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/markdown_site/site.rb', line 220

def generate
    type = @config.type
    unless type == :logseq
        generate_index
    end
    if type == :wiki or type == :logseq or type == :obsidian
        generate_pages
    end
    if type == :blog or type == :logseq
        generate_journals
    end
    generate_knowledge_graph
end

#generate_indexObject



234
235
236
237
238
239
240
# File 'lib/markdown_site/site.rb', line 234

def generate_index
    if @languages
        template = MarkdownSite::RootTemplate.new(self)
        template.generate()
    else
    end
end

#generate_journalsObject



253
254
255
256
# File 'lib/markdown_site/site.rb', line 253

def generate_journals
    template = MarkdownSite::JournalTemplate.new(self)
    template.generate(self.journals)
end

#generate_knowledge_graphObject



258
259
260
261
# File 'lib/markdown_site/site.rb', line 258

def generate_knowledge_graph
    template = MarkdownSite::KnowledgeGraphTemplate.new(self)
    template.generate()
end

#generate_pagesObject



242
243
244
245
246
247
248
249
250
251
# File 'lib/markdown_site/site.rb', line 242

def generate_pages
    template = MarkdownSite::PageTemplate.new(self)
    unless @languages
        template.generate(self.pages)
    else
        @languages.each do |lang|
            template.generate(self.pages(lang[0]), "#{lang[0]}/")
        end
    end
end

#init_citations(type) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/markdown_site/site.rb', line 35

def init_citations(type)
    @citations = MarkdownExtension::Citations.new(@config, type) 
    files = Dir.glob(@pages_path + "/**/*.md")
    files.each do |file|
        unless file == @pages_path + "/summary.md"
            if file.index("hls_")
                @citations.add_inner_citation(file)
            else
                @citations.add_embed_citation(file)
            end
        end
    end
end

#init_journalsObject



110
111
112
113
114
115
116
117
# File 'lib/markdown_site/site.rb', line 110

def init_journals
    @journals = []
    journal_files = Dir.glob(@config.journals + "/*.md")
    journal_files.each do |file|
        page = MarkdownExtension::Page.new(file, self)
        @journals << page
    end
end

#init_pagesObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/markdown_site/site.rb', line 49

def init_pages
    if @languages
        @pages_set = {}
        @languages.each do |lang|
            @pages_set[lang[0]] = init_pages_by_lang("/"+lang[0])
        end
    else
        @pages = init_pages_by_lang
    end
end

#init_pages_by_lang(lang = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/markdown_site/site.rb', line 60

def init_pages_by_lang(lang=nil)
    pages = []
    files = Dir.glob(@pages_path + lang.to_s + "/**/*.md")
    files.sort! do |a,b|
        folder_file_comparison(a,b)
    end
    files.each do |file|
        unless file == @pages_path + lang.to_s + "/summary.md"
            unless file.index("hls_")
                page = MarkdownExtension::Page.new(file, self, lang)
                pages << page
                gen_references(page.item_name , page.markdown)
            end
        end
    end
    return pages
end

#init_publish_dirObject



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/markdown_site/site.rb', line 197

def init_publish_dir
    unless Dir.exist?(@config.publish_dir)
        Dir.mkdir(@config.publish_dir)
    end
    if @languages
        @languages.each do |lang|
            unless Dir.exist?(@config.publish_dir+"/"+lang[0])
                Dir.mkdir(@config.publish_dir+"/"+lang[0])
            end
        end
    end
end

#init_summarysObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/markdown_site/site.rb', line 119

def init_summarys
    if @languages
        @summarys = {}
        @languages.each do |lang|
            @summarys[lang[0]] = MarkdownExtension::Summary.new(@config, lang[0])
        end
    else
        @summary = MarkdownExtension::Summary.new(@config)
    end
end

#summary(lang = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/markdown_site/site.rb', line 130

def summary(lang=nil)
    if @summary
        return @summary
    else
        if @summarys
            if lang
                return @summarys[lang]
            else
                return @summarys[@languages.first[0]]
            end
        end
    end
end

#write_data_jsonObject



189
190
191
192
193
194
195
# File 'lib/markdown_site/site.rb', line 189

def write_data_json
    file = @config.publish_dir + "/data.json"
    data = {"nodes"=>@nodes, "links"=>@links}
    f = File.new(file, "w")
    f.puts JSON.generate(data)
    f.close
end