Skip to content

Instantly share code, notes, and snippets.

@yongbin
Created April 4, 2011 13:34
Show Gist options
  • Select an option

  • Save yongbin/901632 to your computer and use it in GitHub Desktop.

Select an option

Save yongbin/901632 to your computer and use it in GitHub Desktop.
you will be happy in manipulate text paragraph with below .vimrc setting
vnoremap <Leader>k :!knutize<cr>
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
use Text::KnuthPlass;
print knutize(<>);
sub knutize {
my $paragraph = join q{ }, map { join q{ }, split $/, $_ } @_;
my $typesetter = Text::KnuthPlass->new();
my @lines = $typesetter->typeset($paragraph);
my @result;
for (@lines) {
for ( @{ $_->{nodes} } ) {
if ( $_->isa("Text::KnuthPlass::Box") ) { push @result, $_->value }
elsif ( $_->isa("Text::KnuthPlass::Glue") ) { push @result, q{ } }
}
if ( $_->{nodes}[-1]->is_penalty ) { push @result, q{-} }
if ( $result[-1] eq q{ } ) { pop @result }
push @result, $/;
}
return join q{}, @result;
}
@yongbin
Copy link
Copy Markdown
Author

yongbin commented Apr 4, 2011

알려진 문제점

현재 78로 설정된 linelengths는 ascii 에서는 정상적으로 동작하지만 non utf8 환경에서는 한글 1글자를 2byte 처리해 34~6사이에서 행이 잘리는 문제점이, use utf8 환경에서는 한글 1글자를 strlen에서 1 글자로 처리해 한 행에 공백포함 78개 문자가 들어가는 문제점이 각각 존재합니다. 이 문제는 Text::KnuthPlass의 문제라고 보기는 어렵고 한글의 글자폭이 영문보다 큰 특성에 기인한 문제이므로 글자폭을 고려한 적정한 linelengths를 적용하는 방법으로 해결되어야 할것 같습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment