Code: Select all
### Generate Index ###
# This is a sample NWP macro to generate Index for Japanese documents.
# Run against “kusabana.rtf”, it generates Index at the end of the file.
# For other documents, you have to define as many $item as needed.
# This macro indexes a word as “yomi<space>word” of which the yomi works as sort key.
# After the generation of index, “yomi” will be removed with trailing space.
$numColumn = 1 # Number of columns
$removeYomi = @true
$doc = Document.active
if ! $doc
exit 'No open document, exiting...'
end
Debug.setDestination 'new'
$item = Hash.new
# $item{'よみ 索引対象語'} = Cast to String 'Find Expression'
$item{'がんぴ 眼皮'} = Cast to String '眼皮'
$item{'せきちく 石竹'} = Cast to String '石竹'
$item{'だるまだいし 達磨大師'} = Cast to String '達磨大師'
$item{'わかんさんさいずえ 和漢三才図会'} = Cast to String '和漢三才図会'
$item{'かあねいしょん カーネイション'} = Cast to String 'カーネイション'
$item{'ちょうじ 丁子'} = Cast to String '丁子'
$item{'なでしこ ナデシコ'} = Cast to String '(?<!\p{Katakana})ナデシコ'
# 'ナデシコ' not preceded by any Katakana leter (\p{Katakana})
$item{'なでしこ ナデシコ:からなでしこ カラナデシコ'} = Cast to String 'カラナデシコ'
$item{'なでしこ ナデシコ:こなでしこ コナデシコ'} = Cast to String 'コナデシコ'
$item{'なでしこ ナデシコ:むしとりなでしこ ムシトリナデシコ'} = Cast to String 'ムシトリナデシコ'
$item{'ふじわらのさだいえ 藤原定家'} = Cast to String '定家'
$item{'ふじわらのさだいえ 藤原定家:しんちょくせんしゅう 『新勅撰集』'} = Cast to String '新勅撰集'
$item{'ぱあすれい パースレイ'} = Cast to String 'パースレイ'
$item{'ありすとてれす アリストテレス'} = Cast to String 'アリストテレス'
$item{'だりや ダ(ー)リヤ'} = Cast to String 'ダリヤ|ダーリヤ'
# 'ダリヤ' or 'ダーリヤ'
$item{'かゔあにゅす カヴアニュス'} = Cast to String 'カヴアニュス'
$item{'かゔあにゅす カヴアニュス:すぺいんしょくぶつずせつ 西班牙植物図説'} = Cast to String '西班牙植物図説'
$item{'はやしじゅっさい 林述斎'} = Cast to String '林述斎'
$item{'わかつきれいじろう 若槻禮次郞'} = Cast to String '若槻首相'
$item{'きんげんぞう 金源三'} = Cast to String '金源三' # ??? I don’t know
$item{'みなもとのとしより 源俊頼'} = Cast to String '俊頼'
$item{'にほん 日本'} = Cast to String '(?<!\p{Han})日本(?!\p{Han})'
# '日本' not preceded nor followed by any Kanji leter (\p{Han})
$item{'にほん 日本:━じん ━人'} = Cast to String '(?<!\p{Han})日本人(?!\p{Han})'
# '日本人' not preceded nor followed by any Kanji leter (\p{Han})
$item{'にほん 日本:━および━ 「━及━」'} = Cast to String '「日本及日本人」'
$item{''} = Cast to String '' # Empty $item does not affect, it seems
$item{''} = Cast to String ''
$errors = Array.new
foreach $i in $item.keys
$sels = $doc.text.findAll $item{$i}, 'E-i', '-am'
if $sels.count
Push Target Selection $sels
Add to Text Index As $i
Pop Target Selection
else
$errors.appendValue $item{$i}
end
end
# Define cross-references
$crossRef = Hash.new
$crossRef{'ていか 定家'} = Cast to String '藤原定家'
$crossRef{'としより 俊頼'} = Cast to String '源俊頼'
foreach $i in $crossRef.keys
$sel = $doc.text.find $crossRef{$i}, 'E-ir'
$crossRefString = $crossRef{$i}
if $sel
Push Target Selection $sels
Add to Text Index As $i, $crossRefString
Pop Target Selection
else
$errors.appendValue $crossRef{$i}
end
end
# Exclude words in “Not for Index” characrer style from Index
$style = $doc.styleWithName 'Not for Index'
$exclude = $doc.text.findAll $style
if $exclude.count
Push Target Selection $exclude
Remove Text Indexing
Pop Target Selection
end
Select Document End
Document.setActive $doc
Menu.activateAtPath(':Tools:Index:Insert Index')
Send Text $numColumn
Press Button 'Insert'
if $removeYomi
Document.setActive $doc
Replace All '^\S+ ', '', 'EsS-i'
# Remove visible characters at each paragraph start together with trailing space
end
if $errors.count
$errmsg = Cast to String 'Failed: '
foreach $err in $errors
$err = $errmsg & $err
Debug.log $err
end
end
### end of macro