faqml 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Gemfile +2 -0
  2. data/README.markdown +51 -0
  3. data/faqml.gemspec +16 -0
  4. data/lib/faqml.rb +63 -0
  5. metadata +6 -2
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,51 @@
1
+ # FML
2
+
3
+ The FAQ Markup Language. It's pretty simple. The gem is called `faqml`, so first install it.
4
+
5
+ ```ruby
6
+ require 'rubygems'
7
+ require 'faqml'
8
+ data = File.readlines('mypage.fml').join()
9
+ puts FML.new(data).to_html
10
+ ```
11
+
12
+ Your markup in `mypage.fml` should be valid markdown. The only difference is, that QnA blocks are started by three dashes (`---`), and questions and answers are seperated by three equals (`===`). The first line of the question block will be the QnA's title. That's all.
13
+
14
+ This FML
15
+
16
+ ```fml
17
+ ---
18
+ What kind of Bear is Best?
19
+ I hear there are basically two school of thought.
20
+ ===
21
+ False. *Blackbear*.
22
+
23
+ ---
24
+ Do Bears eat Beats?
25
+ ===
26
+ Of course.
27
+ ```
28
+
29
+ Produces this HTML
30
+
31
+ ```html
32
+ <section class="qna">
33
+ <h1><a href="#">What kind of Bear is Best?</a></h1>
34
+ <div class="qna">
35
+ <div class="question">
36
+ <p>I hear there are basically two school of thought.</p>
37
+ </div>
38
+ <div class="answer">
39
+ <p>False. <em>Blackbear</em>.</p>
40
+ </div>
41
+ </div>
42
+ </section>
43
+ <section class="qna">
44
+ <h1><a href="#">Do Bears eat Beats?</a></h1>
45
+ <div class="qna">
46
+ <div class="answer">
47
+ <p>Of course.</p>
48
+ </div>
49
+ </div>
50
+ </section>
51
+ ```
data/faqml.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'faqml'
3
+ s.version = '0.1.1'
4
+ s.date = '2012-08-10'
5
+ s.summary = "FAQ Markup Language"
6
+ s.description = "A Simple FAQ Markup Language, based on Markdown"
7
+ s.authors = ["Eric Redmond"]
8
+ s.email = 'eric@basho.com'
9
+ s.files = ["lib/faqml.rb"]
10
+ s.homepage = 'http://rubygems.org/gems/faqml'
11
+
12
+ s.add_dependency('redcarpet', '>= 2.1.1')
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = ["lib"]
16
+ end
data/lib/faqml.rb ADDED
@@ -0,0 +1,63 @@
1
+ # FAQ Markup Language
2
+ # require 'rubygems'
3
+ require 'redcarpet'
4
+
5
+ class FML
6
+ def initialize(data)
7
+ @data = data
8
+ end
9
+
10
+ def to_html(attrs={})
11
+ linked_question = attrs[:linked_question] || true
12
+
13
+ faqs, current = [], :t
14
+ faqs << {:q => "", :a => "", :t => ""}
15
+ @data.split(/\n/m).each do |line|
16
+ # start an answer block
17
+ if line =~ /^===\s*$/
18
+ raise "Cannot begin an answer without a question title" if current != :q
19
+ current = :a
20
+ next
21
+ end
22
+
23
+ # start a new question block
24
+ if line =~ /^---/
25
+ line = line.sub(/^#/, '').strip
26
+ faqs << {:q => "", :a => "", :t => ""}
27
+ current = :t
28
+ next
29
+ end
30
+
31
+ faqs.last[current] += line
32
+
33
+ current = :q if current == :t && line.strip != ''
34
+ end
35
+
36
+ renderer = Redcarpet::Render::HTML.new
37
+ extensions = {fenced_code_blocks: true}
38
+ redcarpet = Redcarpet::Markdown.new(renderer, extensions)
39
+
40
+ buffer = ""
41
+ for faq in faqs
42
+ next if faq[:t] == ''
43
+ buffer += "<section class=\"qna\">\n"
44
+ if linked_question
45
+ buffer += "<h1><a href=\"#\">#{faq[:t].to_s.strip}</a></h1>\n"
46
+ else
47
+ buffer += "<h1>#{faq[:t].to_s.strip}</h1>\n"
48
+ end
49
+ buffer += "<div class=\"qna\">"
50
+ if faq[:q].to_s != ''
51
+ buffer += "<div class=\"question\">"
52
+ buffer += redcarpet.render(faq[:q])
53
+ buffer += "</div>"
54
+ end
55
+ buffer += "<div class=\"answer\">"
56
+ buffer += redcarpet.render(faq[:a].to_s)
57
+ buffer += "</div>"
58
+ buffer += "</div>"
59
+ buffer += "</section>\n"
60
+ end
61
+ return buffer
62
+ end
63
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faqml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -32,7 +32,11 @@ email: eric@basho.com
32
32
  executables: []
33
33
  extensions: []
34
34
  extra_rdoc_files: []
35
- files: []
35
+ files:
36
+ - Gemfile
37
+ - README.markdown
38
+ - faqml.gemspec
39
+ - lib/faqml.rb
36
40
  homepage: http://rubygems.org/gems/faqml
37
41
  licenses: []
38
42
  post_install_message: