48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/water.rb', line 48
def water diff
output = diff.gsub(/\r\n?/, "\n").scan(/ (?> ^(?!-(?!--\ )|\+(?!\+\+)|[\\ ]|$|@@) .*\n)* (?> ^(?=-(?!--\ )|\+(?!\+\+)|[\\ ]|$|@@) .*(?:\n|\z))+ /x).map do |block|
head_ray, content_ray = CodeRay.scanner(:diff).tokenize(block.split("\n", 2))
content_ray ||= ''
<<-HTML % [head_ray.div(:css => :class), content_ray.div(:css => :class)]
<div class="diff-block">
<div class="diff-block-head">%s</div>
<div class="diff-block-content">%s</div>
</div>
HTML
end.join("\n")
output.extend(CodeRay::Encoders::HTML::Output)
output.css = CodeRay::Encoders::HTML::CSS.new(:alpha)
output.css.stylesheet << Water::CSS
output.wrap_in! CodeRay::Encoders::HTML::Output.page_template_for_css(output.css)
output.apply_title! "diff #{Dir.pwd} | water"
output[/<\/head>\s*<body[^>]*>?/] = <<-JS
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(function () {
$(document).on('click', '.diff-block', function () {
$(this).toggleClass('closed').find('.diff-block-content').slideToggle('fast');
$('html, body').animate({ scrollTop: $(this).offset().top }, 'fast');
});
$(document).on('touchend', '.diff-block', function () {
$(this).toggleClass('closed').find('.diff-block-content').slideToggle('fast');
$('html, body').animate({ scrollTop: $(this).offset().top }, 'fast');
});
$(document).on('click', 'a.toggle-all', function () {
$('.diff-block').toggleClass('closed').find('.diff-block-content').toggle();
});
});
</script>
</head>
<body>
<a href="#" class="toggle-all">toggle all</a>
JS
output
end
|