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::TwitterText::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-1.2.0/lib/tdiary/style/gfm/ 0000775 0000000 0000000 00000000000 13413133265 0020667 5 ustar 00root root 0000000 0000000 tdiary-style-gfm-1.2.0/lib/tdiary/style/gfm/version.rb 0000664 0000000 0000000 00000000126 13413133265 0022700 0 ustar 00root root 0000000 0000000 module TDiary
module Style
module Gfm
VERSION = "1.2.0"
end
end
end
tdiary-style-gfm-1.2.0/spec/ 0000775 0000000 0000000 00000000000 13413133265 0015646 5 ustar 00root root 0000000 0000000 tdiary-style-gfm-1.2.0/spec/spec_helper.rb 0000664 0000000 0000000 00000000632 13413133265 0020465 0 ustar 00root root 0000000 0000000 $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-1.2.0/spec/tdiary/ 0000775 0000000 0000000 00000000000 13413133265 0017142 5 ustar 00root root 0000000 0000000 tdiary-style-gfm-1.2.0/spec/tdiary/style/ 0000775 0000000 0000000 00000000000 13413133265 0020302 5 ustar 00root root 0000000 0000000 tdiary-style-gfm-1.2.0/spec/tdiary/style/gfm_spec.rb 0000664 0000000 0000000 00000031522 13413133265 0022415 0 ustar 00root root 0000000 0000000 # -*- coding: utf-8; -*-
require 'spec_helper'
describe TDiary::Style::GfmDiary do
before do
@diary = TDiary::Style::GfmDiary.new(Time.at( 1041346800 ), "TITLE", "")
end
describe '#append' do
before do
@source = <<-'EOF'
# subTitle
honbun
## subTitleH4
honbun
```
# comment in code block
```
EOF
@diary.append(@source)
end
context 'HTML' do
before do
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
honbun
subTitleH4
honbun
# comment in code block
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
context 'CHTML' do
before do
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
honbun
subTitleH4
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'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "replaceTitle" ) %>
replace
replaceTitleH4
replace
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'autolink' do
before do
source = <<-EOF
# subTitle
* http://www.google.com
[google](https://www.google.com)
http://www.google.com
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 'auto imagelink' do
before do
source = <<-EOF
# subTitle


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 'auto imagelink' do
before do
source = <<-EOF
# subTitle
Anchor
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 'url syntax with code blocks' do
before do
source = <<-'EOF'
# subTitle
```ruby
@foo
```
http://example.com is example.com
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
@foo
http://example.com is example.com
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'ignored url syntax with markdown anchor' do
before do
source = <<-'EOF'
# subTitle
[example](http://example.com) is example.com
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
example is example.com
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'plugin syntax' do
before do
source = <<-'EOF'
# subTitle
{{plugin 'val'}}
{{plugin "val", 'val'}}
{{plugin <
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
<%=plugin 'val'%>
<%=plugin "val", 'val'%>
<%=plugin <
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'plugin syntax with url args' do
before do
source = <<-'EOF'
# subTitle
{{plugin 'http://www.example.com/foo.html', "https://www.example.com/bar.html"}}
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
<%=plugin 'http://www.example.com/foo.html', "https://www.example.com/bar.html"%>
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'link to my plugin' do
before do
source = <<-'EOF'
# subTitle
[](20120101p01)
[Link](20120101p01)
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
<%=my "20120101p01", "20120101p01" %>
<%=my "20120101p01", "Link" %>
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'code highlighting' do
before do
source = <<-'EOF'
# subTitle
```ruby
def class
@foo = 'bar'
end
```
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
def class
@foo = 'bar'
end
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
describe 'ignore emphasis' do
before do
source = <<-'EOF'
# subTitle
@a_matsuda is amatsuda
{{isbn_left_image ''}}
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
@a_matsuda is amatsuda
<%=isbn_left_image ''%>
<%=section_leave_proc( Time.at( 1041346800 ) )%>
EOF
end
it { expect(@diary.to_html).to eq @html }
end
context 'twitter username' do
describe 'in plain context' do
before do
source = <<-'EOF'
# subTitle
@a_matsuda is amatsuda
EOF
@diary.append(source)
@html = <<-'EOF'
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %>
@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
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-1.2.0/tdiary-style-gfm.gemspec 0000664 0000000 0000000 00000002152 13413133265 0021462 0 ustar 00root root 0000000 0000000 # coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'tdiary/style/gfm/version'
Gem::Specification.new do |spec|
spec.name = "tdiary-style-gfm"
spec.version = TDiary::Style::Gfm::VERSION
spec.authors = ["SHIBATA Hiroshi"]
spec.email = ["hsbt@ruby-lang.org"]
spec.description = %q{GFM Style for tDiary}
spec.summary = %q{GFM Style for tDiary}
spec.homepage = "https://github.com/tdiary/tdiary-style-gfm"
spec.license = "GPL-3.0"
spec.required_ruby_version = ">= 2.2.2"
spec.files = Dir["README.md", "LICENSE.txt", "tdiary-style-gfm.gemspec", "lib/**/*"]
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_dependency 'commonmarker'
spec.add_dependency 'twitter-text', '>= 2.0'
spec.add_dependency 'emot'
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
end