Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] satysfibuilder #1205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions lib/review/satysfibuilder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# satysfibuilder.rb
#
# Copyright (c) 2018 Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
#

require 'review/builder'

module ReVIEW
class SATYSFIBuilder < Builder
HEADLINE = {
1 => 'chapter',
2 => 'section',
3 => 'subsection'
}.freeze

def pre_paragraph
'+p{'
end

def post_paragraph
'}'
end

def extname
'.saty'
end

def builder_init_file
@section = 0
@subsection = 0

@indent = ' '
end
private :builder_init_file

def escape(str)
str.gsub('\\', '\\\\\\').
gsub(';', '\\;').
gsub('<', '\\<').
gsub('>', '\\>').
gsub('{', '\\{').
gsub('}', '\\}')
end

def unescape(str)
str.gsub('\\;', ';').
gsub('\\<', '<').
gsub('\\>', '>').
gsub('\\{', '{').
gsub('\\}', '}').
gsub('\\\\\\', '\\')
end

def headline(level, _label, caption)
case level
when 2
puts ' >' if @subsection > 0
puts ' >' if @section > 0
@section += 1
@subsection = 0
when 3
puts ' >' if @subsection > 0
@subsection += 1
end

headline_name = HEADLINE[level]
@indent = level
puts %Q(#{indents}+#{headline_name}{#{compile_inline(caption)}}<)
@indent += 1
end

def result
s = ''
if @subsection > 0
@indent -= 1
s += "#{indents}>\n"
end
if @section > 0
@indent -= 1
s += "#{indents}>\n"
end
@indent -= 1
s += "#{indents}>\n" # chapter
@output.string + s
end

def paragraph(lines)
puts "#{indents}#{pre_paragraph}"
@indent += 1
lines.each do |line|
puts "#{indents}#{line}"
end
@indent -= 1
puts "#{indents}#{post_paragraph}"
end

def inline_b(s)
"\\emph{#{escape(s)}}"
end

def inline_tt(s)
# needs local.satyh
"\\file{#{escape(s)}}"
end
alias_method :inline_code, :inline_tt

def footnote(_id, _str)
# handle by inline_fn
end

def inline_fn(id)
%Q(\\footnote{#{compile_inline(@chapter.footnote(id).content.strip)}})
end

def inline_img(id)
_chapter, id = extract_chapter_id(id)
%Q(\\ref(\`fig:#{escape(id)}\`);)
end

def image_image(id, caption, metric)
puts <<EOT
#{indents}+p{
#{indents} \\figure ?:(`fig:#{escape(id)}`){#{compile_inline(caption)}}<
#{indents} +image-frame{\\insert-image(#{metric})(`#{@chapter.image(id).path}`);}
#{indents} >
#{indents}}
EOT
end

def indents
' ' * @indent
end

def nofunc_text(str)
escape(str)
end
end
end