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.4"

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)
  <<-"CODE"
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(function() {
  $("*").contents().filter(function() {
return this.nodeType==8 && this.nodeValue.match(/^#{name}/);
  }).each(function(i, e) {
var tooltips = e.nodeValue.replace(/^#{name}\s*[\\n\\r]|[\\n\\r]$/g, '');
$(this).prev().attr('title', tooltips);
  });
});
</script>
CODE
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)
  <<-"CODE"
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(function() {
  $("*").contents().filter(function() {
return this.nodeType==8 && this.nodeValue.match(/^#{name}/);
  }).each(function(i, e) {
var tooltips = e.nodeValue.replace(/^#{name} *[\\n\\r]|[\\n\\r]$/g, '');
var link = "<span><a href='#' onclick='javascript:return false;' class='toggleLink'>" + "#{toggle_link_text[0]}" + "</a></span>";
$(this).prev().append(link);
$(this).prev().after("<pre style='display:none'>"+ tooltips + "</pre>");
  });

  $('.toggleLink').click(
function() {
  if ($(this).text()=="#{toggle_link_text[0]}") {
   $(this).parent().parent().next('pre').slideDown(200);
   $(this).text("#{toggle_link_text[1]}");
  } else {
    $(this).parent().parent().next('pre').slideUp(200);
    $(this).text("#{toggle_link_text[0]}");
  };
});
});
</script>
CODE
end