markdown_extension 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d67b05d598435c2741847c8fd952529bafb52c81a2e44fe4d777dc411fd5249
4
- data.tar.gz: 73b80afa3d1b87336fbdbe65292f15c76a85464b75d32e70f4040afe9abbd900
3
+ metadata.gz: 4bb43ea339db198a6e04e1bdcbe4575951eeb94b6f0e1db19569044b4c4834da
4
+ data.tar.gz: c74cf7d6755e9fcbfbc70c87df4b707da3b03428b06c4b11c22d2beb5c059784
5
5
  SHA512:
6
- metadata.gz: 8805b839dfa692bc6c5f4d1c391f618a7b6c71a383642de6036891afe4b5e6763234aed3081a0bf6d8696b7a590f0d2ede4a73099228c4dd6141207c8facbe77
7
- data.tar.gz: 0a32b12acdc87d9237d4cdc1027da9a94b45cd12ec4680c22baaeab52ed87f36a5fd3df3cf2b2ead5fcfe737fd2a49dc5d2ece17c7eda06fcaacf8161bb44260
6
+ metadata.gz: 6a7b2012acbdc22faa5f4f4166d08666739d97795cd1824d965ccb045ef889d20837cc53a67e4b091cbaea65e8401888bc450fb73879ca59319322dd28082728
7
+ data.tar.gz: c3b585038c0e0efc7eb4c61c0a6fa11699c766de78bf61dd13e1b6983a9caf46476692fdb823786c384e6a7d0b2a0cb694ab68e4faa1a8c9ef786b80b031c1fa
@@ -0,0 +1,46 @@
1
+ require "tomlrb"
2
+
3
+ module MarkdownExtension
4
+ class Config
5
+ attr_accessor :raw_config, :file, :type
6
+
7
+ def load_file(file, type)
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 , type)
19
+ return self
20
+ end
21
+
22
+ def title
23
+ if @raw_config
24
+ if @raw_config[@type.to_s]
25
+ return @raw_config[@type.to_s]["title"]
26
+ end
27
+ end
28
+ ""
29
+ end
30
+
31
+ def src
32
+ if @raw_config
33
+ if @raw_config[@type.to_s]
34
+ return @raw_config[@type.to_s]["src"]
35
+ end
36
+ end
37
+ ""
38
+ end
39
+
40
+ def preprocessing
41
+ if @raw_config
42
+ return @raw_config["preprocessing"]
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,72 @@
1
+ require "kramdown"
2
+ require "kramdown-parser-gfm"
3
+ require "tomlrb"
4
+
5
+ module MarkdownExtension
6
+ class Page
7
+ attr_accessor :site, :markdown, :meta, :item_name
8
+
9
+ def initialize(file, site)
10
+ @site = site
11
+ if File.exists?(file)
12
+ @markdown = File.read(file)
13
+ else
14
+ @markdown = ""
15
+ end
16
+ if @markdown.start_with?("---\n")
17
+ mds = @markdown.split("---\n")
18
+ @meta = mds[1]
19
+ @markdown = mds[2..-1].join("---\n")
20
+ end
21
+ @item_name = file.split("/")[-1].gsub(".md","")
22
+ end
23
+
24
+ def pre_processing
25
+ if @site.config.preprocessing["backlinks"]
26
+ @markdown = @markdown.gsub(/\[\[(.*)\]\]/) do |s|
27
+ s = s[2..-3]
28
+ "[#{s}](#{s}.html)"
29
+ end
30
+ if @site.references[@item_name]
31
+ @markdown += "\n\n\n"
32
+ @markdown += "### References\n"
33
+ @site.references[@item_name].each do |item|
34
+ @markdown += "* [#{item}](#{item}.html)\n"
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def html
41
+ pre_processing()
42
+ @page_html = Kramdown::Document.new(@markdown, input: 'GFM').to_html
43
+ return @page_html
44
+ end
45
+
46
+ def meta_html
47
+ meta_data = Tomlrb.parse(@meta)
48
+ html = ""
49
+ meta_data.each do |title, values|
50
+ html += "<p class=\"text-center bg-primary font-size-14\">"+title+"</p>\n"
51
+ html += "<table class=\"table font-size-12\">\n"
52
+ values.each do |key, value|
53
+ html += "<tr>\n"
54
+ if value.class == String
55
+ html += "<td>"+key + "</td><td>" + value.to_s + "</td>\n"
56
+ elsif value.class == Array
57
+ html += "<td>"+key + "</td><td>" + value.join("<br />") + "</td>\n"
58
+ elsif value.class == Hash
59
+ values = ""
60
+ value.each do |k,v|
61
+ values += k+": "+v+"<br />"
62
+ end
63
+ html += "<td>"+key + "</td><td>" + values + "</td>\n"
64
+ end
65
+ html += "</tr>\n"
66
+ end
67
+ html += "</table>\n"
68
+ end
69
+ return html
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,82 @@
1
+ require "json"
2
+
3
+ module MarkdownExtension
4
+ class Site
5
+ attr_accessor :config, :summary, :pages, :references, :reverse_references
6
+ attr_accessor :nodes, :links
7
+
8
+ def initialize(config, type)
9
+ @config = MarkdownExtension::Config.new(config, type)
10
+ @summary = MarkdownExtension::Summary.new(@config)
11
+ @references = {}
12
+ @reverse_references = {}
13
+ @nodes = []
14
+ @links = []
15
+ load_source_files()
16
+ gen_nodes_links()
17
+ end
18
+
19
+ def load_source_files
20
+ @pages = []
21
+ if @config
22
+ files = Dir.glob(@config.src + "/*.md")
23
+ files.each do |file|
24
+ unless file == @config.src + "/summary.md"
25
+ page = MarkdownExtension::Page.new(file, self)
26
+ pages << page
27
+ gen_references(file , page.markdown)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def gen_references(file, text)
34
+ text.gsub(/\[\[(.*)\]\]/) do |s|
35
+ s = s[2..-3]
36
+ item_name = file.split("/")[-1].gsub(".md","")
37
+ if @references[s]
38
+ @references[s] << item_name
39
+ else
40
+ @references[s] = [item_name]
41
+ end
42
+ if @reverse_references[item_name]
43
+ @reverse_references[item_name] << s
44
+ else
45
+ @reverse_references[item_name] = [s]
46
+ end
47
+ end
48
+ end
49
+
50
+ def gen_nodes_links
51
+ @references.each do |k,v|
52
+ @nodes << {
53
+ "id" => k,
54
+ "name" => k,
55
+ "color" => "blue",
56
+ "val" => @reverse_references[k] ? @reverse_references[k].size+1 : 1
57
+ }
58
+ v.each do |item|
59
+ @nodes << {
60
+ "id" => item,
61
+ "name" => item,
62
+ "color" => "blue",
63
+ "val" => @reverse_references[item] ? @reverse_references[item].size+1 : 1
64
+ }
65
+ @links << {
66
+ "source" => item,
67
+ "target" => k
68
+ }
69
+ end
70
+ end
71
+ @nodes = @nodes.uniq
72
+ @links = @links.uniq
73
+ end
74
+
75
+ def write_data_json(file)
76
+ data = {"nodes"=>@nodes, "links"=>@links}
77
+ f = File.new(file, "w")
78
+ f.puts JSON.generate(data)
79
+ f.close
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,31 @@
1
+ require "kramdown"
2
+ require "kramdown-parser-gfm"
3
+
4
+ module MarkdownExtension
5
+ class Summary
6
+ attr_accessor :config, :markdown
7
+ def initialize(config)
8
+ @config = config
9
+ file = config.src+"/summary.md"
10
+ if File.exists?(file)
11
+ @markdown = File.read(file)
12
+ else
13
+ @markdown = ""
14
+ end
15
+ end
16
+
17
+ def pre_processing
18
+ if @config.preprocessing["backlinks"]
19
+ @markdown = @markdown.gsub(/\[\[(.*)\]\]/) do |s|
20
+ s = s[2..-3]
21
+ "[#{s}](#{s}.html)"
22
+ end
23
+ end
24
+ end
25
+
26
+ def html
27
+ pre_processing()
28
+ return Kramdown::Document.new(@markdown, input: 'GFM').to_html
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module MarkdownExtension
2
- Version = '0.0.3'
2
+ Version = '0.0.4'
3
3
  end
@@ -0,0 +1 @@
1
+ Dir[File.dirname(__FILE__) + '/markdown_extension/*.rb'].each {|file| require file }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_extension
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhuang Biaowei
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-22 00:00:00.000000000 Z
11
+ date: 2022-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tomlrb
@@ -59,6 +59,11 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - lib/markdown_extension.rb
63
+ - lib/markdown_extension/config.rb
64
+ - lib/markdown_extension/page.rb
65
+ - lib/markdown_extension/site.rb
66
+ - lib/markdown_extension/summary.rb
62
67
  - lib/markdown_extension/version.rb
63
68
  homepage: https://github.com/markdown-world/markdown_extension
64
69
  licenses: