wst-parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0715e64d017d578d777b7b80499d77d64ad1e20f
4
+ data.tar.gz: b7f3baee2c62193ce19f26c016b49cbc9bdec03f
5
+ SHA512:
6
+ metadata.gz: abaa6f5439914b4f56002a998d00c64010939734cb454903a700a4e999f016f3e44cc36076235cc0f358f9721047a1ce4fdaaed7ad89570a68a7f620e5a2c48f
7
+ data.tar.gz: 80720f5770b4d342a8dc1bb46de8189b1239213f7f3d35f9af26564e3ffa6225987f0c8382087011cefbb74406490796b7aa2c8dbae03f60a9a2072659dab563
data/bin/wst-parser ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'wst/parser'
4
+
5
+ dir = ARGV[0] || Dir.pwd
6
+
7
+ Wst::Configuration.read_config dir, false
8
+
9
+ puts "Pages:"
10
+ Wst::Page.all.each do |page|
11
+ puts "\t" + page.content_url
12
+ end
13
+ puts "Posts:"
14
+ Wst::Post.all.each do |post|
15
+ puts "\t" + post.content_url
16
+ end
data/lib/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+
11
+ [*.{sh,markdown}]
12
+ indent_size = 4
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+
3
+ module Wst
4
+ module Configuration
5
+ def config
6
+ Configuration.config
7
+ end
8
+
9
+ def self.config
10
+ @config
11
+ end
12
+
13
+ def defaultLinks
14
+ Configuration.defaultLinks
15
+ end
16
+
17
+ def self.defaultLinks
18
+ @defaultLinks
19
+ end
20
+
21
+ def self.read_config location, local
22
+ Configuration.read_configuration location, local
23
+ Configuration.read_default_links
24
+ Configuration.read_translations
25
+ end
26
+
27
+ def self.read_configuration location, local
28
+ raise "Not a valid Web Log Today location" unless self.valid_location? location
29
+ @config = YAML.load File.open(File.join(location, 'config.yaml'), 'r:utf-8').read
30
+ @config["__site_url__"] = @config["site_url"]
31
+ @config["site_url"] = "http://localhost:4000" if local
32
+ @config["path"] = location
33
+ @config["debug"] = ENV['WST_ENV'] != nil && ENV['WST_ENV'].casecmp('debug').zero?
34
+ end
35
+
36
+ def self.read_default_links
37
+ @defaultLinks = if File.exists? self.links_file_path then "\n" + File.open(self.links_file_path, "r:utf-8").read else "" end
38
+ end
39
+
40
+ def self.read_translations
41
+ @config['translations'] = {}
42
+ self.translation_files.each do |file|
43
+ translation = YAML.load File.open(file, 'r:utf-8').read
44
+ lang = File.basename file, '.yaml'
45
+ @config['translations'][lang] = translation
46
+ end
47
+ end
48
+
49
+ def self.valid_location? location
50
+ return false unless File.exists? File.join location, "config.yaml"
51
+ return false unless File.directory? File.join location, "_posts"
52
+ return false unless File.directory? File.join location, "_pages"
53
+ return false unless File.directory? File.join location, "_layouts"
54
+ return true
55
+ end
56
+
57
+ def self.links_file_path
58
+ File.join config['path'], "links.md"
59
+ end
60
+
61
+ def self.translation_files
62
+ Dir.glob File.join config['path'], "_translations", "**.yaml"
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+ require 'yaml'
3
+ require 'wst/configuration'
4
+ require 'digest/md5'
5
+
6
+ module Wst
7
+ class Content
8
+ include Configuration
9
+
10
+ def initialize file_path, child = nil
11
+ @file_path = file_path
12
+ @plain_content = ""
13
+ @datas = Hash.new
14
+ @cats = ""
15
+ @child = child
16
+ @content = content
17
+
18
+ read_content
19
+ end
20
+
21
+ def child
22
+ @child
23
+ end
24
+
25
+ def content
26
+ c = self
27
+ while !c.child.nil?
28
+ c = c.child
29
+ end
30
+ c
31
+ end
32
+
33
+ def dir
34
+ File.dirname @file_path
35
+ end
36
+
37
+ def method_missing(m, *args, &block)
38
+ if m =~ /^(.*)\?$/
39
+ return @datas.has_key? $1
40
+ elsif @datas.has_key? m.to_s
41
+ return @datas[m.to_s]
42
+ else
43
+ return nil
44
+ end
45
+ end
46
+
47
+ def raw_content
48
+ @plain_content
49
+ end
50
+
51
+ def content_url= url
52
+ @url = url
53
+ end
54
+
55
+ def content_url
56
+ @url ||= ''
57
+ end
58
+
59
+ def datas
60
+ @datas
61
+ end
62
+
63
+ def gravatar?
64
+ email?
65
+ end
66
+
67
+ def gravatar
68
+ hash = Digest::MD5.hexdigest(email)
69
+ "http://www.gravatar.com/avatar/#{hash}"
70
+ end
71
+
72
+ def account? name
73
+ return false unless config.has_key? "accounts"
74
+ return config["accounts"].has_key? name
75
+ end
76
+
77
+ def account name
78
+ return nil unless account? name
79
+ return config["accounts"][name]
80
+ end
81
+
82
+ protected
83
+
84
+ def default_links_path
85
+ "#{config['path']}/links.md"
86
+ end
87
+
88
+ def read_content
89
+ @plain_content = File.open(@file_path, "r:utf-8").read
90
+ begin
91
+ if @plain_content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
92
+ @plain_content = $'
93
+ @datas = YAML.load $1
94
+ end
95
+ rescue => e
96
+ puts "YAML Exception reading #{@file_path}: #{e.message}"
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require 'wst/content'
3
+
4
+ module Wst
5
+ class HamlContent < Content
6
+ def initialize file_path, child = nil, sub_content = ''
7
+ super file_path, child
8
+ @sub_content = sub_content
9
+ end
10
+
11
+ def sub_content
12
+ @sub_content
13
+ end
14
+
15
+ def deep_content
16
+ c = self
17
+ while !c.child.nil? && !c.child.child.nil?
18
+ c = c.child
19
+ end
20
+ c
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+ require 'wst/content'
3
+ require 'wst/configuration'
4
+
5
+ module Wst
6
+ class MdContent < Content
7
+ @@matcher = /^(.+\/)*(\d+-\d+-\d+)?-?(.*)(\.[^.]+)$/
8
+
9
+ def initialize file_path
10
+ super file_path
11
+
12
+ m, cats, date, slug, ext = *file_path.match(@@matcher)
13
+ @cats = cats
14
+ @date = Time.parse(date) if date
15
+ @slug = slug
16
+ @ext = ext
17
+ end
18
+
19
+ def date
20
+ @date
21
+ end
22
+
23
+ def raw_content
24
+ if useDefaultLinks?
25
+ content_with_links
26
+ else
27
+ @plain_content
28
+ end
29
+ end
30
+
31
+ def self.matcher
32
+ @@matcher
33
+ end
34
+
35
+ protected
36
+
37
+ def content_with_links
38
+ @plain_content + defaultLinks
39
+ end
40
+
41
+ def useDefaultLinks?
42
+ if @datas.has_key? 'nolinks'
43
+ @datas['nolinks'] == 'true'
44
+ else
45
+ true
46
+ end
47
+ end
48
+ end
49
+ end
data/lib/wst/page.rb ADDED
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+ require 'wst/md_content'
3
+ require 'wst/haml_content'
4
+ require 'cgi'
5
+
6
+ module Wst
7
+ module PageComparison
8
+ def <=> obj
9
+ return content_url <=> obj.content_url
10
+ end
11
+ end
12
+
13
+ class HamlPage < HamlContent
14
+ include PageComparison
15
+
16
+ @@matcher = /^(.+\/)*(.*)(\.[^.]+)$/
17
+
18
+ def initialize file_path
19
+ super file_path
20
+
21
+ m, cats, slug, ext = *file_path.match(@@matcher)
22
+ base_path = File.join(Configuration.config['path'], '_pages') + '/'
23
+ @cats = cats.gsub(base_path, '').chomp('/') if !cats.nil? && cats != base_path
24
+ @slug = slug
25
+ @ext = ext
26
+ end
27
+
28
+ def content_url
29
+ "#{@cats + '/' if @cats != ''}#{CGI.escape @slug}.html"
30
+ end
31
+
32
+ def self.matcher
33
+ @@matcher
34
+ end
35
+ end
36
+
37
+ class MdPage < MdContent
38
+ include PageComparison
39
+
40
+ def initialize file_path
41
+ super file_path
42
+
43
+ base_path = File.join(Configuration.config['path'], '_pages') + '/'
44
+ @cats = @cats.gsub(base_path, '') if !@cats.nil?
45
+ @cats.chomp!('/')
46
+ end
47
+
48
+ def content_url
49
+ "#{@cats + '/' if @cats != ''}#{CGI.escape @slug}.html"
50
+ end
51
+ end
52
+
53
+ class Page
54
+ @@glob_md = '*.{md,mkd,markdown}'
55
+ @@glob_haml = '*.haml'
56
+
57
+ class << self
58
+ def all
59
+ (haml_pages + md_pages).sort
60
+ end
61
+
62
+ private
63
+
64
+ def haml_pages
65
+ page_files(@@glob_haml, HamlPage.matcher).inject([]) { |pages, file| pages << HamlPage.new(file) }
66
+ end
67
+
68
+ def md_pages
69
+ page_files(@@glob_md, MdContent.matcher).inject([]) { |pages, file| pages << MdPage.new(file) }
70
+ end
71
+
72
+ def page_files glob, matcher
73
+ select_match all_files(glob), matcher
74
+ end
75
+
76
+ def select_match files, matcher
77
+ files.select { |file| file =~ matcher }
78
+ end
79
+
80
+ def all_files glob
81
+ Dir.glob glob_path glob
82
+ end
83
+
84
+ def glob_path glob
85
+ File.join "#{Configuration.config['path']}/_pages", '**', glob
86
+ end
87
+ end
88
+ end
89
+ end
data/lib/wst/parser.rb ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ require 'wst/configuration'
4
+ require 'wst/page'
5
+ require 'wst/post'
data/lib/wst/post.rb ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ require 'wst/md_content'
3
+
4
+ module Wst
5
+ class Post < MdContent
6
+ @@glob = "*.{md,mkd,markdown}"
7
+
8
+ def initialize file_path
9
+ super file_path
10
+
11
+ base_path = File.join(Configuration.config['path'], '_posts') + '/'
12
+ @cats = @cats.gsub(base_path, '') if !@cats.nil?
13
+ @cats.chomp!('/')
14
+ end
15
+
16
+ def content_url
17
+ generate_url = {
18
+ "year" => @date.strftime("%Y"),
19
+ "month" => @date.strftime("%m"),
20
+ "day" => @date.strftime("%d"),
21
+ "title" => CGI.escape(@slug)
22
+ }.inject(":year/:month/:day/:title.html") { |result, token|
23
+ result.gsub(/:#{Regexp.escape token.first}/, token.last)
24
+ }.gsub(/\/\//, "/")
25
+ generate_url = "#{@cats}/#{generate_url}" if @cats != ''
26
+ generate_url
27
+ end
28
+
29
+ class << self
30
+ def all
31
+ post_files.inject([]) { |posts, file| posts << Post.new(file) }
32
+ end
33
+
34
+ private
35
+
36
+ def glob_path
37
+ File.join "#{Configuration.config['path']}/_posts", '**', @@glob
38
+ end
39
+
40
+ def all_files
41
+ Dir.glob glob_path
42
+ end
43
+
44
+ def select_match files
45
+ files.select { |file| file =~ @@matcher }
46
+ end
47
+
48
+ def post_files
49
+ select_match(all_files).sort
50
+ end
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wst-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yves Brissaud
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Parse content of a wst instance
14
+ email: yves.brissaud@gmail.com
15
+ executables:
16
+ - wst-parser
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/wst-parser
21
+ - lib/.editorconfig
22
+ - lib/wst/configuration.rb
23
+ - lib/wst/content.rb
24
+ - lib/wst/haml_content.rb
25
+ - lib/wst/md_content.rb
26
+ - lib/wst/page.rb
27
+ - lib/wst/parser.rb
28
+ - lib/wst/post.rb
29
+ homepage: https://github.com/eunomie/wst-parser
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.4.5.1
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Web Site Today Parser
53
+ test_files: []