Last active
August 16, 2019 05:45
-
-
Save kmuto/430c0e5d0f0c50250372491fd56042a3 to your computer and use it in GitHub Desktop.
指定の幅で折り返す実装。Re:VIEW masterブランチの環境で動作する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ReVIEW | |
module LATEXBuilderOverride | |
# gem install unicode_display_width | |
require 'unicode/display_width' | |
require 'unicode/display_width/string_ext' | |
CR = '→' # 送り出し文字。LaTeXコードも可 | |
ZWSCALE = 0.875 # 和文・欧文の比率。\setlength{\xkanjiskip}{\z@} しておいたほうがよさそう | |
def split_line(s, n) | |
# 文字列を幅nで分割 | |
a = [] | |
l = '' | |
w = 0 | |
s.each_char do |c| | |
cw = c.display_width(2) # Ambiguousを全角扱い | |
cw *= ZWSCALE if cw == 2 | |
if w + cw > n | |
a.push(l) | |
l = c | |
w = cw | |
else | |
l << c | |
w += cw | |
end | |
end | |
a.push(l) | |
a | |
end | |
def code_line(type, line, idx, id, caption, lang) | |
# _typeには'emlist'などが入ってくるので、環境に応じて分岐は可能 | |
n = 64 | |
n = 60 if @doc_status[:column] | |
a = split_line(unescape(detab(line)), n) | |
# インラインopはこの時点でもう展開されたものが入ってしまっているので、escapeでエスケープされてしまう… | |
escape(a.join("\x01\n")).gsub("\x01", CR) + "\n" | |
end | |
def code_line_num(type, line, first_line_num, idx, id, caption, lang) | |
n = 60 | |
n = 56 if @doc_status[:column] | |
a = split_line(unescape(detab(line)), n) | |
(idx + first_line_num).to_s.rjust(2) + ': ' + escape(a.join("\x01\n ")).gsub("\x01", CR) + "\n" | |
end | |
end | |
class LATEXBuilder | |
prepend LATEXBuilderOverride | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment