metatext 0.0.1
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 +7 -0
- data/lib/metatext.rb +79 -0
- metadata +45 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: b763b82cda785d08a7ff084f7daf8d5ae04782fa
|
|
4
|
+
data.tar.gz: f558d114f7ed525ae132e25dbe6a99eb4ee58292
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7a9b5726ccf394e58f4d038203f2aebe025021b95df48f87a843afb3bc820994ca6cc247d02e9145706d9880d831ef077564f30e7b2d0527f12d60558213e4e8
|
|
7
|
+
data.tar.gz: 25c9e6780c4bc2d6bacad6cc7c04b2f589430f5d487af8d363cf4856f5977c72374f35e5dd1326c21eca5008e2f4e394599390ed623a29d54e7ecd55c73a69da
|
data/lib/metatext.rb
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
require 'erb'
|
|
3
|
+
require 'ostruct'
|
|
4
|
+
|
|
5
|
+
# MetaText
|
|
6
|
+
# a lightweight jekyll-style metadata parser
|
|
7
|
+
#
|
|
8
|
+
# [what it do]
|
|
9
|
+
#
|
|
10
|
+
# * parses yaml at the top of any file, jekyll style
|
|
11
|
+
# * stores metadata in an object
|
|
12
|
+
# * inject your own text processor (e.g. redcarpet for markdown)
|
|
13
|
+
# * use erb (<% %>) for dynamic text on demand
|
|
14
|
+
#
|
|
15
|
+
# @author jguest
|
|
16
|
+
|
|
17
|
+
class Metatext
|
|
18
|
+
class << self
|
|
19
|
+
|
|
20
|
+
# @param dir - path to metatext files
|
|
21
|
+
# @param ext - the file extention (e.g. 'txt', 'md', 'txt.erb')
|
|
22
|
+
|
|
23
|
+
def configure(dir:, ext:, processor: nil)
|
|
24
|
+
@dir = dir
|
|
25
|
+
@ext = ext
|
|
26
|
+
@pro = processor
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# main driver method for metatext parsing
|
|
30
|
+
# @return self
|
|
31
|
+
|
|
32
|
+
def parse(file, locals={})
|
|
33
|
+
raw = read file
|
|
34
|
+
raw = erbify raw, with: locals if @ext.include? 'erb'
|
|
35
|
+
yield metadata(raw), render(raw)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
# opens and reads the metatext file
|
|
41
|
+
# @param file
|
|
42
|
+
|
|
43
|
+
def read(file)
|
|
44
|
+
File.read "#{@dir}/#{file.to_s}.#{@ext}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# run the file contents through erb
|
|
48
|
+
# @param with - the variables you want available in the file
|
|
49
|
+
|
|
50
|
+
def erbify(raw, with:)
|
|
51
|
+
namespace = OpenStruct.new with
|
|
52
|
+
ERB.new(raw).result namespace.instance_eval { binding }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# get metadata with yaml in-between "---"
|
|
56
|
+
# @param raw
|
|
57
|
+
|
|
58
|
+
def metadata(raw)
|
|
59
|
+
raw.match data_regex
|
|
60
|
+
OpenStruct.new YAML.load($1) if $1
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# get content with everything but yaml section
|
|
64
|
+
# @param raw
|
|
65
|
+
|
|
66
|
+
def render(raw)
|
|
67
|
+
content = raw.gsub data_regex, "" || raw
|
|
68
|
+
return @pro.render content if @pro
|
|
69
|
+
content
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# a regex for "---- some metadata --- the real content"
|
|
73
|
+
# @return regular expression
|
|
74
|
+
|
|
75
|
+
def data_regex
|
|
76
|
+
/^(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: metatext
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- John Guest
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-09-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: a lightweight jekyll-style metadata parser
|
|
14
|
+
email: guest.john88@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/metatext.rb
|
|
20
|
+
homepage: http://rubygems.org/gems/metatext
|
|
21
|
+
licenses:
|
|
22
|
+
- MIT
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubyforge_project:
|
|
40
|
+
rubygems_version: 2.4.6
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: metatext
|
|
44
|
+
test_files: []
|
|
45
|
+
has_rdoc:
|