Module: Togglate

Defined in:
lib/togglate.rb,
lib/togglate/cli.rb,
lib/togglate/version.rb

Defined Under Namespace

Classes: BlockWrapper, CLI

Constant Summary collapse

VERSION =
"0.1.6"

Class Method Summary collapse

Class Method Details

.append_code(method, opts) ⇒ Object



28
29
30
# File 'lib/togglate.rb', line 28

def append_code(method, opts)
  send("#{method}_code", opts)
end

.commentout(text, tag: 'original') ⇒ Object Also known as: comment_out



19
20
21
22
23
24
25
# File 'lib/togglate.rb', line 19

def commentout(text, tag:'original')
  comments = []
  comment_re = /\n?^<!--#{tag}\n(.*?)^-->\n?/m

  remains = text.gsub(comment_re) { |m| comments << $1; '' }
  return comments*"\n", remains
end

.create(text, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/togglate.rb', line 9

def create(text, opts={})
  wrapped = BlockWrapper.new(text, opts).run
  if opts[:embed_code]
    code = append_code(opts[:method], opts)
    "#{wrapped}\n#{code}"
  else
    wrapped
  end
end

.hover_code(name: 'original', **opts) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/togglate.rb', line 61

def hover_code(name:'original', **opts)
  "<script src=\"http://code.jquery.com/jquery-1.11.0.min.js\"></script>\n<script>\n$(function() {\n  $(\"*\").contents().filter(function() {\nreturn this.nodeType==8 && this.nodeValue.match(/^\#{name}/);\n  }).each(function(i, e) {\nvar tooltips = e.nodeValue.replace(/^\#{name}\\s*[\\\\n\\\\r]|[\\\\n\\\\r]$/g, '');\n$(this).prev().attr('title', tooltips);\n  });\n});\n</script>\n"
end

.toggle_code(name: 'original', toggle_link_text: ["*", "hide"], **opts) ⇒ Object



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
# File 'lib/togglate.rb', line 32

def toggle_code(name:'original', toggle_link_text:["*", "hide"], **opts)
  "<script src=\"http://code.jquery.com/jquery-1.11.0.min.js\"></script>\n<script>\n$(function() {\n  $(\"*\").contents().filter(function() {\nreturn this.nodeType==8 && this.nodeValue.match(/^\#{name}/);\n  }).each(function(i, e) {\nvar tooltips = e.nodeValue.replace(/^\#{name} *[\\\\n\\\\r]|[\\\\n\\\\r]$/g, '');\nvar link = \"<span><a href='#' onclick='javascript:return false;' class='toggleLink'>\" + \"\#{toggle_link_text[0]}\" + \"</a></span>\";\n$(this).prev().append(link);\n$(this).prev().after(\"<pre style='display:none'>\"+ tooltips + \"</pre>\");\n  });\n\n  $('.toggleLink').click(\nfunction() {\n  if ($(this).text()==\"\#{toggle_link_text[0]}\") {\n   $(this).parent().parent().next('pre').slideDown(200);\n   $(this).text(\"\#{toggle_link_text[1]}\");\n  } else {\n    $(this).parent().parent().next('pre').slideUp(200);\n    $(this).text(\"\#{toggle_link_text[0]}\");\n  };\n});\n});\n</script>\n"
end