Class: MarkdownExtension::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_extension/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, site, lang = nil) ⇒ Page

Returns a new instance of Page.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/markdown_extension/page.rb', line 9

def initialize(file, site, lang=nil)
    @site = site
    if File.exist?(file)
        @markdown = File.read(file)
    else
        @markdown = ""
    end
    if @markdown.start_with?("---\n")
        mds = @markdown.split("---\n")
        @meta = mds[1]
        @markdown = mds[2..-1].join("---\n")
    end
    @path = file.gsub(site.pages_path+lang.to_s+"/","").gsub(".md","")
    @item_name = file.split("/")[-1].gsub(".md","")
    @ctime = File::ctime(file)
    @mtime = File::mtime(file)
end

Instance Attribute Details

#ctimeObject

Returns the value of attribute ctime.



7
8
9
# File 'lib/markdown_extension/page.rb', line 7

def ctime
  @ctime
end

#item_nameObject

Returns the value of attribute item_name.



7
8
9
# File 'lib/markdown_extension/page.rb', line 7

def item_name
  @item_name
end

#markdownObject

Returns the value of attribute markdown.



7
8
9
# File 'lib/markdown_extension/page.rb', line 7

def markdown
  @markdown
end

#metaObject

Returns the value of attribute meta.



7
8
9
# File 'lib/markdown_extension/page.rb', line 7

def meta
  @meta
end

#mtimeObject

Returns the value of attribute mtime.



7
8
9
# File 'lib/markdown_extension/page.rb', line 7

def mtime
  @mtime
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/markdown_extension/page.rb', line 7

def path
  @path
end

#siteObject

Returns the value of attribute site.



7
8
9
# File 'lib/markdown_extension/page.rb', line 7

def site
  @site
end

Instance Method Details

#htmlObject



82
83
84
85
86
# File 'lib/markdown_extension/page.rb', line 82

def html
    pre_processing()
    @page_html = Kramdown::Document.new(@markdown, input: 'GFM').to_html
    return @page_html
end

#meta_htmlObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/markdown_extension/page.rb', line 88

def meta_html
    return nil unless @meta
     = Tomlrb.parse(@meta)
    html = ""
    .each do |title, values|
        html += "<p class=\"text-center bg-primary font-size-14\">"+title+"</p>\n"
        html += "<table class=\"table font-size-12\">\n"
        values.each do |key, value|
            html += "<tr>\n"
            if value.class == String
                html += "<td>"+key + "</td><td>" + value.to_s + "</td>\n"
            elsif value.class == Array
                html += "<td>"+key + "</td><td>" + value.join("<br />") + "</td>\n"
            elsif value.class == Hash
                values = ""
                value.each do |k,v|
                    values += k+": "+v+"<br />"
                end
                html += "<td>"+key + "</td><td>" + values + "</td>\n"
            end
            html += "</tr>\n"
        end
        html += "</table>\n"
    end
    return html
end

#pre_processingObject



27
28
29
30
31
32
33
34
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/markdown_extension/page.rb', line 27

def pre_processing
    if @site.config.preprocessing["backlinks"]
        @markdown = @markdown.gsub(/\[\[([^\]]+)\]\]/) do |s|
            index = @markdown.index(s)
            if (@markdown[index-1]=="(" && @markdown[index+s.size]==")")
                s = s[2..-3]
                "#{s}.html"
            else
                s = s[2..-3]
                "[#{s}](#{s}.html)"
            end
        end
        if @site.references[@item_name]
            @markdown += "\n\n\n"
            @markdown += "### References\n"
            @site.references[@item_name].each do |item|
                @markdown += "* [#{item}](#{item}.html)\n"
            end
        end
    end
    if @site.config.type == :logseq
        @markdown.gsub!(/\t/, "    ")
        if @markdown[-1]=="-"
            @markdown = @markdown[0..-2]
        end
        @markdown.gsub!("(../assets/", "(./assets/")
        # @markdown.gsub!(/(.+)collapsed:: true\n/, "")
        @markdown = @markdown.gsub(/.+([0-9]+\. )/) do |s|
            s.gsub(".","")
        end
        while (i = @markdown.index(":LOGBOOK:")) do 
            j = @markdown.index(":END:", i)
            while(@markdown[i]!="\n") do
                i = i - 1
            end
            @markdown=@markdown[0..i-1] + @markdown[j+5..-1]
        end
        @markdown = @markdown.gsub(/{{embed \(\(([^\)]+)\)\)}}/) do |s|
            id = s[10..-5]
            "embed #{id}: \n" + @site.citations.get_inner_citation(id)
        end
        @markdown = @markdown.gsub(/\(\([^\)]+\)\)/) do |s|
            if s.index(":")
                id = s
            else
                id = s[2..-3]                        
            end
            @site.citations.get_inner_citation(id)
        end
        @markdown = @markdown.split("\n").collect {|line|
            (line.strip.index("id:: ") && line.strip.length==41)? nil : line                    
        }.compact.join("\n")
    end
end