honbun
honbun
# comment in code block
<%=section_leave_proc( Time.at( 1041346800 ) )%>
honbun
honbun
# comment in code block
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html({}, :CHTML)).to eq @html }
end
context 'to_src' do
it { expect(@diary.to_src).to eq @source }
end
end
describe '#replace' do
before do
source = <<-'EOF'
# subTitle
honbun
## subTitleH4
honbun
EOF
@diary.append(source)
replaced = <<-'EOF'
# replaceTitle
replace
## replaceTitleH4
replace
EOF
@diary.replace(Time.at( 1041346800 ), "TITLE", replaced)
@html = <<-'EOF'
replace
replace
<%=section_leave_proc( Time.at( 1041346800 ) )%>

@foo
http://example.com is example.com
<%=section_leave_proc( Time.at( 1041346800 ) )%>example is example.com
<%=section_leave_proc( Time.at( 1041346800 ) )%><%=plugin 'val'%>
<%=plugin "val", 'val'%>
<%=plugin <
<%=plugin 'http://www.example.com/foo.html', "https://www.example.com/bar.html"%>
<%=section_leave_proc( Time.at( 1041346800 ) )%><%=my "20120101p01", "20120101p01" %>
<%=my "20120101p01", "Link" %>
<%=section_leave_proc( Time.at( 1041346800 ) )%> def class
@foo = 'bar'
end
@a_matsuda is amatsuda
<%=isbn_left_image ''%>
<%=section_leave_proc( Time.at( 1041346800 ) )%>@a_matsuda is amatsuda
<%=section_leave_proc( Time.at( 1041346800 ) )%>' do
before do
source = <<-'EOF'
# subTitle
p :some_code
@a_matsuda is amatsuda
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
p :some_code
@a_matsuda is amatsuda
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'with ' do
before do
source = <<-'EOF'
# subTitle
`:some_code`
@a_matsuda is amatsuda
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
:some_code
@a_matsuda is amatsuda
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
end
context 'emoji' do
describe 'in plain context' do
before do
source = <<-'EOF'
# subTitle
:sushi: は美味しい
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
は美味しい
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'in (multiline) ' do
before do
source = <<-'EOF'
# subTitle
```
:sushi: は
美味しい
```
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
:sushi: は
美味しい
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'in ' do
before do
source = <<-'EOF'
# subTitle
`:sushi:` は美味しい
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
:sushi: は美味しい
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'in (with attribute)' do
before do
source = <<-'EOF'
# subTitle
:sushi: は美味しい
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
:sushi: は美味しい
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
end
describe 'do not modify original string' do
before do
@orig_source = <<-'EOF'
# subTitle
{{fn 'テスト'}}"
EOF
@source = @orig_source.dup
@diary.append(@source)
@diary.to_html
@section = nil
@diary.each_section{|x| @section = x}
end
it { expect(@section.body).to eq("\n"+@orig_source.lines.to_a.last+"\n") }
end
describe 'stashes in pre, code, plugin' do
before do
source = <<-'EOF'
# subTitle
```
ruby -e "puts \"hello, world.\""
```
`ruby -e "puts \"hello, world.\""`
{{plugin "\0", "\1", "\2"}}
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
ruby -e "puts \"hello, world.\""
ruby -e "puts \"hello, world.\""
<%=plugin "\0", "\1", "\2"%>
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'plugin syntax in pre, code block' do
before do
source = <<-'EOF'
# subTitle
Get IP Address of Docker Container:
```
% docker inspect -f "{{.NetworkSettings.IPAddress}} {{.Config.Hostname}} # Name:{{.Name}}" `docker ps -q`
```
NOTE: `{{.NetworkSettings.IPAddress}}` is golang template.
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
Get IP Address of Docker Container:
% docker inspect -f "{{.NetworkSettings.IPAddress}} {{.Config.Hostname}} # Name:{{.Name}}" `docker ps -q`
NOTE: {{.NetworkSettings.IPAddress}} is golang template.
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
end
# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End:
tdiary-style-gfm-0.5.1/spec/spec_helper.rb 0000644 0001750 0001750 00000000632 13151117140 020315 0 ustar uwabami uwabami $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'tdiary/core_ext'
require 'tdiary/comment_manager'
require 'tdiary/referer_manager'
require 'tdiary/style'
require 'tdiary/style/gfm'
TDiary::Style::GfmDiary.send(:include, TDiary::Style::BaseDiary)
TDiary::Style::GfmDiary.send(:include, TDiary::Style::CategorizableDiary)
TDiary::Style::GfmSection.send(:include, TDiary::Style::BaseSection)
tdiary-style-gfm-0.5.1/.travis.yml 0000644 0001750 0001750 00000000255 13151117140 016657 0 ustar uwabami uwabami language: ruby
rvm:
- 2.2
- 2.3
- 2.4
- ruby-head
matrix:
allowed_failures:
- rvm: ruby-head
before_install: gem update bundler
script: bundle exec rake spec
tdiary-style-gfm-0.5.1/lib/ 0000755 0001750 0001750 00000000000 13151117140 015312 5 ustar uwabami uwabami tdiary-style-gfm-0.5.1/lib/tdiary/ 0000755 0001750 0001750 00000000000 13151117140 016606 5 ustar uwabami uwabami tdiary-style-gfm-0.5.1/lib/tdiary/style/ 0000755 0001750 0001750 00000000000 13151117140 017746 5 ustar uwabami uwabami tdiary-style-gfm-0.5.1/lib/tdiary/style/gfm/ 0000755 0001750 0001750 00000000000 13151117140 020517 5 ustar uwabami uwabami tdiary-style-gfm-0.5.1/lib/tdiary/style/gfm/version.rb 0000644 0001750 0001750 00000000126 13151117140 022530 0 ustar uwabami uwabami module TDiary
module Style
module Gfm
VERSION = "0.5.1"
end
end
end
tdiary-style-gfm-0.5.1/lib/tdiary/style/gfm.rb 0000644 0001750 0001750 00000013562 13151117140 021053 0 ustar uwabami uwabami # -*- coding: utf-8; -*-
require 'github/markdown'
require 'rouge'
require 'twitter-text'
module TDiary
module Style
class GfmSection
def initialize(fragment, author = nil)
@author = author
@subtitle, @body = fragment.split(/\n/, 2)
@subtitle.sub!(/^\#\s*/,'')
@body ||= ''
@categories = get_categories
@stripped_subtitle = strip_subtitle
@subtitle_to_html = @subtitle ? to_html('# ' + @subtitle).gsub(/\A|<\/h\d>\z/io, '') : nil
@stripped_subtitle_to_html = @stripped_subtitle ? to_html('# ' + @stripped_subtitle).gsub(/\A|<\/h\d>\z/io, '') : nil
@body_to_html = to_html(@body)
end
def subtitle=(subtitle)
@subtitle = (subtitle || '').sub(/^# /,"\##{categories_to_string} ")
@strip_subtitle = strip_subtitle
end
def categories=(categories)
@subtitle = "#{categories_to_string} " + (strip_subtitle || '')
@strip_subtitle = strip_subtitle
end
def to_src
r = ''
r << "\# #{@subtitle}\n" if @subtitle
r << @body
end
def do_html4(date, idx, opt)
subtitle = to_html('# ' + @subtitle)
subtitle.sub!( %r!(.+?)
!m ) do
"<%= subtitle_proc( Time.at( #{date.to_i} ), #{$1.dump.gsub( /%/, '\\\\045' )} ) %>
"
end
if opt['multi_user'] and @author then
subtitle.sub!(/<\/h3>/,%Q|[#{@author}]|)
end
r = subtitle
r << @body_to_html
end
private
def to_html(string)
r = string.dup
# 1. Stash plugin calls
plugin_stashes = []
plugin_blocks, replaced_r = extract_plugin_blocks_and_replace(r)
plugin_blocks.each do |plugin_block|
# Convert `{{ }}' to erb tags
plugin_stashes.push(["{{#{plugin_block}}}", "<%=#{plugin_block}%>"])
end
r = replaced_r
# 2. Apply markdown conversion
r = GitHub::Markdown.to_html(r, :gfm) do |code, lang|
begin
formatter = Rouge::Formatters::HTMLPygments.new(Rouge::Formatters::HTML.new, 'highlight')
lexer = Rouge::Lexer.find_fancy(lang)
formatter.format(lexer.lex(code))
rescue Exception => ex
"#{CGI.escapeHTML(code)}"
end
end
# 3. Stash and tags
pre_tag_stashes = []
r.gsub!(//m) do |matched|
pre_tag_stashes.push(matched)
"@@tdiary_style_gfm_pre_tag#{pre_tag_stashes.length - 1}@@"
end
code_tag_stashes = []
r.gsub!(//m) do |matched|
code_tag_stashes.push(matched)
"@@tdiary_style_gfm_code_tag#{code_tag_stashes.length - 1}@@"
end
# 4. Convert miscellaneous
if pre_tag_stashes.none? && code_tag_stashes.none?
r = Twitter::Autolink.auto_link_usernames_or_lists(r)
end
r = r.emojify
# diary anchor
r.gsub!(/(.*?)<\/a>/) {
unless $3.empty?
%Q|<%=my "#{$1}#{$2}", "#{$3}" %>|
else
%Q|<%=my "#{$1}#{$2}", "#{$1}#{$2}" %>|
end
}
# 5. Unstash , and plugin call
pre_tag_stashes.each.with_index do |str, i|
plugin_stashes.each.with_index do |(p_str, p_erb), j|
if str["@@tdiary_style_gfm_plugin#{j}@@"]
str["@@tdiary_style_gfm_plugin#{j}@@"] = CGI.escapeHTML(p_str)
end
end
r["@@tdiary_style_gfm_pre_tag#{i}@@"] = str
end
code_tag_stashes.each.with_index do |str, i|
plugin_stashes.each.with_index do |(p_str, p_erb), j|
if str["@@tdiary_style_gfm_plugin#{j}@@"]
str["@@tdiary_style_gfm_plugin#{j}@@"] = CGI.escapeHTML(p_str)
end
end
r["@@tdiary_style_gfm_code_tag#{i}@@"] = str
end
plugin_stashes.each.with_index do |(str, erb), i|
if r["@@tdiary_style_gfm_plugin#{i}@@"]
r["@@tdiary_style_gfm_plugin#{i}@@"] = erb
end
end
r
end
def get_categories
return [] unless @subtitle
cat = /(\\?\[([^\[]+?)\\?\])+/.match(@subtitle).to_a[0]
return [] unless cat
cat.scan(/\\?\[(.*?)\\?\]/).collect do |c|
c[0].split(/,/)
end.flatten
end
def strip_subtitle
return nil unless @subtitle
r = @subtitle.sub(/^((\\?\[[^\[]+?\]\\?)+\s+)?/, '')
if r.empty?
nil
else
r
end
end
def extract_plugin_blocks_and_replace(text)
s = StringScanner.new(text)
blocks = []
count = 0
replaced_text = ""
while chunk = s.scan_until(/\{\{/)
chunk[-2, 2] = ""
replaced_text << chunk
if plugin_str = extract_plugin_block(s)
replaced_text << "@@tdiary_style_gfm_plugin#{count}@@"
blocks << plugin_str
count += 1
end
end
replaced_text << s.rest
return blocks, replaced_text
end
def extract_plugin_block(s)
pos = s.pos
buf = ""
while chunk = s.scan_until(/\}\}/)
buf << chunk
buf.chomp!("}}")
return buf
end
s.pos = pos
nil
end
end
class GfmDiary
def initialize(date, title, body, modified = Time.now)
init_diary
replace( date, title, body )
@last_modified = modified
end
def style
'GFM'
end
def append(body, author = nil)
in_code_block = false
section = nil
body.each_line do |l|
case l
when /^\#[^\#]/
if in_code_block
section << l
else
@sections << GfmSection.new(section, author) if section
section = l
end
when /^```/
in_code_block = !in_code_block
section << l
else
section = '' unless section
section << l
end
end
if section
section << "\n" unless section =~ /\n\n\z/
@sections << GfmSection.new(section, author)
end
@last_modified = Time.now
self
end
def add_section(subtitle, body)
@sections = GfmSection.new("\# #{subtitle}\n\n#{body}")
@sections.size
end
end
end
end
# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End:
tdiary-style-gfm-0.5.1/lib/tdiary-style-gfm.rb 0000644 0001750 0001750 00000000033 13151117140 021034 0 ustar uwabami uwabami require 'tdiary/style/gfm'
tdiary-style-gfm-0.5.1/README.md 0000644 0001750 0001750 00000002163 13151117140 016025 0 ustar uwabami uwabami # TDiary::Style::Gfm
[](https://rubygems.org/gems/tdiary-style-gfm) [](https://travis-ci.org/tdiary/tdiary-style-gfm) [](https://codeclimate.com/github/tdiary/tdiary-style-gfm)
"GitHub Flavored Markdown" (GFM) style for tDiary 2.x format.
## Installation
Add this line to your tDiary's Gemfile.local:
gem 'tdiary-style-gfm'
And then execute:
$ bundle
## Usage
if you want to use this style, add @style into tdiary.conf below:
@style = 'GFM'
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## Copyright
* Copyright (C) 2003, TADA Tadashi
* Copyright (C) 2004, MoonWolf
* Copyright (C) 2012, kdmsnr
* Copyright (C) 2013, hsbt