markdown_site 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe0956d2f10ba9491c573f1c4059b713dc718ed2ed3c9795ef09ba3aa17b2673
4
+ data.tar.gz: 5030d58915fc8fd522847bb7fc1ecbb6e11bb7c6c349f1665853e4246c916e16
5
+ SHA512:
6
+ metadata.gz: 542486a4e71920fb43c58966d957db601f90d90b75083da7ad651a47b9032592912285d2cfa8561067085558029981cccb68b5b73d9baca9daa3344f164ac223
7
+ data.tar.gz: 56052865acca00e2d7d06446a7ee5e3134fab0508185c68e7c3f5d0b7dd9ae4bb31d3bede4d513032fb85a0b3bb39cffd06ea5acd133713ef88e1490df709579
@@ -0,0 +1,119 @@
1
+ require "tomlrb"
2
+
3
+ module MarkdownSite
4
+ class Config
5
+ attr_accessor :raw_config, :file, :type
6
+
7
+ def load_file(file)
8
+ @raw_config = begin
9
+ Tomlrb.load_file(file)
10
+ rescue
11
+ {}
12
+ end
13
+ end
14
+
15
+ def initialize(file, type)
16
+ @file = file
17
+ @type = type
18
+ load_file(file)
19
+ return self
20
+ end
21
+
22
+ def get_base_info(name)
23
+ if @raw_config
24
+ if @raw_config[@type.to_s]
25
+ return @raw_config[@type.to_s][name]
26
+ end
27
+ end
28
+ ""
29
+ end
30
+
31
+ def get_generate_info(name)
32
+ if @raw_config["generate"]
33
+ return @raw_config["generate"][name]
34
+ end
35
+ end
36
+
37
+ def get_template_info(name)
38
+ if @raw_config["template"]
39
+ return @raw_config["template"][name]
40
+ end
41
+ end
42
+
43
+ def title
44
+ get_base_info("title")
45
+ end
46
+
47
+ def src
48
+ get_base_info("src")
49
+ end
50
+
51
+ def pages
52
+ get_base_info("pages")
53
+ end
54
+
55
+ def journals
56
+ get_base_info("journals")
57
+ end
58
+
59
+ def preprocessing
60
+ if @raw_config
61
+ return @raw_config["preprocessing"]
62
+ end
63
+ end
64
+
65
+ def giscus
66
+ if @raw_config
67
+ return @raw_config["giscus"]
68
+ end
69
+ end
70
+
71
+ def citation
72
+ if @raw_config
73
+ return @raw_config["citation"]
74
+ end
75
+ end
76
+
77
+ def languages
78
+ if @raw_config
79
+ return @raw_config["languages"]
80
+ end
81
+ end
82
+
83
+ def publish_dir
84
+ return get_generate_info("publish_dir")
85
+ end
86
+
87
+ def assets_dir
88
+ return get_generate_info("assets_dir")
89
+ end
90
+
91
+ def copy_files
92
+ return get_generate_info("copy_files")
93
+ end
94
+
95
+ def knowledge_graph
96
+ return get_generate_info("knowledge_graph")
97
+ end
98
+
99
+ def knowledge_graph_template
100
+ return get_template_info("knowledge_graph_template")
101
+ end
102
+
103
+ def pages_template
104
+ return get_template_info("pages_template")
105
+ end
106
+
107
+ def page_template
108
+ return get_template_info("page_template")
109
+ end
110
+
111
+ def journal_template
112
+ return get_template_info("journal_template")
113
+ end
114
+
115
+ def journals_template
116
+ return get_template_info("journals_template")
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,237 @@
1
+ require "json"
2
+ require "liquid"
3
+ require "markdown_extension"
4
+
5
+ module MarkdownSite
6
+ class Site
7
+ attr_accessor :config, :pages, :journals, :references, :reverse_references
8
+ attr_accessor :nodes, :links, :citations, :languages
9
+
10
+ def initialize(config, type)
11
+ @references = {}
12
+ @reverse_references = {}
13
+ @nodes = []
14
+ @links = []
15
+ @config = MarkdownSite::Config.new(config, type)
16
+ @languages = @config.languages
17
+ if @config.type == :logseq
18
+ @pages_path = @config.pages
19
+ else
20
+ @pages_path = @config.src
21
+ end
22
+ init_citations(type)
23
+ if type == :wiki or type == :logseq or type == :obsidian
24
+ init_pages
25
+ end
26
+ if type == :blog or type == :logseq
27
+ init_journals
28
+ end
29
+ if type == :wiki or type == :obsidian
30
+ init_summarys
31
+ end
32
+ gen_nodes_links
33
+ end
34
+
35
+ def init_citations(type)
36
+ @citations = MarkdownExtension::Citations.new(@config, type)
37
+ files = Dir.glob(@pages_path + "/**/*.md")
38
+ files.each do |file|
39
+ unless file == @pages_path + "/summary.md"
40
+ if file.index("hls_")
41
+ @citations.add_inner_citation(file)
42
+ else
43
+ @citations.add_embed_citation(file)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ def init_pages
50
+ if @languages
51
+ @pages_set = {}
52
+ @languages.each do |lang|
53
+ @pages_set[lang[0]] = init_pages_by_lang("/"+lang[0])
54
+ end
55
+ else
56
+ @pages = init_pages_by_lang
57
+ end
58
+ end
59
+
60
+ def init_pages_by_lang(lang=nil)
61
+ pages = []
62
+ files = Dir.glob(@pages_path + lang.to_s + "/**/*.md")
63
+ files.each do |file|
64
+ unless file == @pages_path + lang.to_s + "/summary.md"
65
+ unless file.index("hls_")
66
+ page = MarkdownExtension::Page.new(file, self)
67
+ pages << page
68
+ gen_references(page.item_name , page.markdown)
69
+ end
70
+ end
71
+ end
72
+ return pages
73
+ end
74
+
75
+ def pages(lang=nil)
76
+ if @pages
77
+ return @pages
78
+ else
79
+ if @pages_set
80
+ if lang
81
+ return @pages_set[lang]
82
+ else
83
+ return @pages_set[@languages.first[0]]
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ def init_journals
90
+ @journals = []
91
+ journal_files = Dir.glob(@config.journals + "/*.md")
92
+ journal_files.each do |file|
93
+ page = MarkdownExtension::Page.new(file, self)
94
+ @journals << page
95
+ end
96
+ end
97
+
98
+ def init_summarys
99
+ if @languages
100
+ @summarys = {}
101
+ @languages.each do |lang|
102
+ @summarys[lang[0]] = MarkdownExtension::Summary.new(@config, lang[0])
103
+ end
104
+ else
105
+ @summary = MarkdownExtension::Summary.new(@config)
106
+ end
107
+ end
108
+
109
+ def summary(lang=nil)
110
+ if @summary
111
+ return @summary
112
+ else
113
+ if @summarys
114
+ if lang
115
+ return @summarys[lang]
116
+ else
117
+ return @summarys[@languages.first[0]]
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ def gen_references(item_name, text)
124
+ text.gsub(/\[\[([^\]]+)\]\]/) do |s|
125
+ s = s[2..-3]
126
+ if @references[s]
127
+ @references[s] << item_name
128
+ else
129
+ @references[s] = [item_name]
130
+ end
131
+ if @reverse_references[item_name]
132
+ @reverse_references[item_name] << s
133
+ else
134
+ @reverse_references[item_name] = [s]
135
+ end
136
+ end
137
+ end
138
+
139
+ def gen_nodes_links
140
+ @references.each do |k,v|
141
+ val = @references[k] ? @references[k].size+1 : 1
142
+ val = 5 if val > 5
143
+ @nodes << {
144
+ "id" => k,
145
+ "name" => k,
146
+ "color" => "blue",
147
+ "val" => val
148
+ }
149
+ v.each do |item|
150
+ val = @references[item] ? @references[item].size+1 : 1
151
+ val = 5 if val > 5
152
+ @nodes << {
153
+ "id" => item,
154
+ "name" => item,
155
+ "color" => "blue",
156
+ "val" => val
157
+ }
158
+ @links << {
159
+ "source" => item,
160
+ "target" => k
161
+ }
162
+ end
163
+ end
164
+ @nodes = @nodes.uniq
165
+ @links = @links.uniq
166
+ end
167
+
168
+ def write_data_json
169
+ file = @config.publish_dir + "/data.json"
170
+ data = {"nodes"=>@nodes, "links"=>@links}
171
+ f = File.new(file, "w")
172
+ f.puts JSON.generate(data)
173
+ f.close
174
+ end
175
+
176
+ def init_publish_dir
177
+ unless Dir.exist?(@config.publish_dir)
178
+ Dir.mkdir(@config.publish_dir)
179
+ end
180
+ if @languages
181
+ @languages.each do |lang|
182
+ unless Dir.exist?(@config.publish_dir+"/"+lang[0])
183
+ Dir.mkdir(@config.publish_dir+"/"+lang[0])
184
+ end
185
+ end
186
+ end
187
+ end
188
+
189
+ def copy_assets
190
+ init_publish_dir
191
+ if Dir.exist?(@config.assets_dir)
192
+ `cp -r #{@config.assets_dir} #{@config.publish_dir}/#{@config.assets_dir}`
193
+ @config.copy_files.each do |file|
194
+ `cp #{file} #{@config.publish_dir}/#{file}`
195
+ end
196
+ end
197
+ end
198
+
199
+ def generate
200
+ type = @config.type
201
+ unless type == :logseq
202
+ generate_index
203
+ end
204
+ if type == :wiki or type == :logseq or type == :obsidian
205
+ generate_pages
206
+ end
207
+ if type == :blog or type == :logseq
208
+ generate_journals
209
+ end
210
+ generate_knowledge_graph
211
+ end
212
+
213
+ def generate_index
214
+ end
215
+
216
+ def generate_pages
217
+ template = MarkdownSite::PageTemplate.new(self)
218
+ unless @languages
219
+ template.generate(self.pages)
220
+ else
221
+ @languages.each do |lang|
222
+ template.generate(self.pages(lang[0]), "#{lang[0]}/")
223
+ end
224
+ end
225
+ end
226
+
227
+ def generate_journals
228
+ template = MarkdownSite::JournalTemplate.new(self)
229
+ template.generate(self.journals)
230
+ end
231
+
232
+ def generate_knowledge_graph
233
+ template = MarkdownSite::KnowledgeGraphTemplate.new(self)
234
+ template.generate()
235
+ end
236
+ end
237
+ end
@@ -0,0 +1,45 @@
1
+ module MarkdownSite
2
+ class Template
3
+ attr_accessor :site_config
4
+
5
+ def initialize(site)
6
+ @site = site
7
+ @site_config = site.config
8
+ end
9
+
10
+ def get_pagination(url, count, number)
11
+ pagination = {}
12
+ pagination["pages"] = []
13
+ if number == 1
14
+ f = File.new("#{@site_config.publish_dir}/#{url}.html", "w")
15
+ pagination["previous"]="#"
16
+ pagination["next"]="#{url}_#{number+1}.html"
17
+ elsif number == 2
18
+ f = File.new("#{@site_config.publish_dir}/#{url}_#{number}.html", "w")
19
+ pagination["previous"]="#{url}.html"
20
+ pagination["next"]="#{url}_#{number+1}.html"
21
+ elsif number == count
22
+ f = File.new("#{@site_config.publish_dir}/#{url}_#{number}.html", "w")
23
+ pagination["previous"]="#{url}_#{number-1}.html"
24
+ pagination["next"]="#"
25
+ else
26
+ f = File.new("#{@site_config.publish_dir}/#{url}_#{number}.html", "w")
27
+ pagination["previous"]="#{url}_#{number-1}.html"
28
+ pagination["next"]="#{url}_#{number+1}.html"
29
+ end
30
+ 1.upto(count) do |p_i|
31
+ if p_i == 1
32
+ page_url = "#{url}.html"
33
+ else
34
+ page_url = "#{url}_#{p_i}.html"
35
+ end
36
+ if number == p_i
37
+ pagination["pages"] << {"active"=>true, "url"=>page_url, "number"=>"#{p_i}"}
38
+ else
39
+ pagination["pages"] << {"active"=>false, "url"=>page_url, "number"=>"#{p_i}"}
40
+ end
41
+ end
42
+ return pagination, f
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module MarkdownSite
2
+ class KnowledgeGraphTemplate < Template
3
+ def initialize(site)
4
+ super(site)
5
+ end
6
+ def generate
7
+ kg_template = Liquid::Template.parse(File.read(@site_config.knowledge_graph_template))
8
+ f = File.new(@site_config.publish_dir + @site_config.knowledge_graph, "w")
9
+ f.puts(kg_template.render('config'=>{'title'=>@site_config.title}))
10
+ f.close
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ module MarkdownSite
2
+ class JournalTemplate < Template
3
+ def initialize(site)
4
+ super(site)
5
+ end
6
+ def generate(site_journals)
7
+ journal_list = []
8
+ site_journals.reverse.each do |journal|
9
+ journal_template = Liquid::Template.parse(File.read(@site_config.journal_template))
10
+ journal_html = journal_template.render('title'=>journal.item_name, 'content'=>journal.html)
11
+ journal_list << journal_html
12
+ end
13
+
14
+ template = Liquid::Template.parse(File.read(@site_config.journals_template))
15
+ page_count = (journal_list.size / 20) + 1
16
+ 1.upto(page_count) do |page_number|
17
+ pagination, f = get_pagination("index", page_count, page_number)
18
+ page_start = (page_number-1)*20
19
+ if page_number == page_count
20
+ f.puts(template.render('config'=>{'title'=>@site_config.title}, 'journal_list'=>journal_list[page_start..-1], 'pagination'=>pagination))
21
+ else
22
+ f.puts(template.render('config'=>{'title'=>@site_config.title}, 'journal_list'=>journal_list[page_start..page_start+20], 'pagination'=>pagination))
23
+ end
24
+ f.close
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,51 @@
1
+ module MarkdownSite
2
+ class PageTemplate < Template
3
+ def initialize(site)
4
+ super(site)
5
+ end
6
+
7
+ def add_page(page)
8
+ if @site.references[page.item_name]
9
+ return {
10
+ "item_name"=>page.item_name,
11
+ "references"=>@site.references[page.item_name].size,
12
+ "ctime"=>page.ctime,
13
+ "mtime"=>page.mtime
14
+ }
15
+ else
16
+ return {"item_name"=>page.item_name, "references"=>0, "ctime"=>page.ctime, "mtime"=>page.mtime}
17
+ end
18
+ end
19
+
20
+ def generate(site_pages, lang=nil)
21
+ template = Liquid::Template.parse(File.read(@site_config.pages_template))
22
+ page_count = (site_pages.size / 20) + 1
23
+ 1.upto(page_count) do |page_number|
24
+ pagination, f = get_pagination("#{lang}pages", page_count, page_number)
25
+ page_start = (page_number-1)*20
26
+ pages = []
27
+ if page_number == page_count
28
+ site_pages[page_start..-1].each do |page|
29
+ pages << add_page(page)
30
+ end
31
+ else
32
+ site_pages[page_start..page_start+20].each do |page|
33
+ pages << add_page(page)
34
+ end
35
+ end
36
+ f.puts(template.render('config'=>{'title'=>@site_config.title}, 'pages'=>pages, 'pagination'=>pagination))
37
+ f.close
38
+ end
39
+ template = Liquid::Template.parse(File.read(@site_config.page_template))
40
+ site_pages.each do |page|
41
+ filename = "#{@site_config.publish_dir}/#{lang}#{page.item_name}.html"
42
+ f = File.new(filename, "w")
43
+ f.puts template.render(
44
+ 'config'=>{'title'=>@site_config.title},
45
+ 'page_title' => page.item_name,
46
+ 'page_html' => page.html)
47
+ f.close
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module MarkdownSite
2
+ Version = '0.0.2'
3
+ end
@@ -0,0 +1 @@
1
+ Dir[File.dirname(__FILE__) + '/markdown_site/**/*.rb'].each {|file| require file }
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown_site
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Zhuang Biaowei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tomlrb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: markdown_extension
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: liquid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.4.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.4.0
55
+ description:
56
+ email:
57
+ - zbw@kaiyuanshe.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/markdown_site.rb
63
+ - lib/markdown_site/config.rb
64
+ - lib/markdown_site/site.rb
65
+ - lib/markdown_site/template.rb
66
+ - lib/markdown_site/templates/Knowledge_graph_template.rb
67
+ - lib/markdown_site/templates/journal_template.rb
68
+ - lib/markdown_site/templates/page_template.rb
69
+ - lib/markdown_site/version.rb
70
+ homepage: https://github.com/markdown-world/markdown_site
71
+ licenses:
72
+ - Apache-2.0
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.5.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.4.6
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A markdown-based site management module.
93
+ test_files: []