NISUS Archives

February 2011

NISUS@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Reply To:
Date:
Sun, 27 Feb 2011 03:48:39 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (105 lines)
On 2011-02-26 [+0900 JST], at 8:51 PM, Kino wrote:

> 		if $replaced == undefined
> 			$n = $i + 1
> 			$error = "Default Note Text for note $n ($type) could not be removed.\n" & $error
> 		end

Bad code which will show something meaningless when $error is not empty. I corrected the macro and uploaded it as
<http://www2.odn.ne.jp/alt-quinon/files/NWPro/footendnotes/Note2Body_20110227_nwm.zip>

Sorry for that kind of thoughtless code of mine . . . as always !?! ;-)


Kino
___

### Note2Body macro (rev 19) ###

# This macro will produce a new document where all footnotes and end/section notes will be converted into inline notes enclosed by tags such as \footnote{...}.

# The tags are customizable. Define $fnPrefix, $fnSuffix, $enPrefix and $enSuffix near the beigining of macro code as you like.

# Characters between Note References and Note Text will be removed if they are the same as Default Note Text defined in the Style Sheet.

Require Pro Version 1.3
Debug.setCodeProfilingEnabled false
Debug.setDestination 'none'
$prefix = $suffix = $defaultNT = $count = Hash.new

$prefix{'footnotes'} = Cast to String '\footnote{'
$suffix{'footnotes'} = Cast to String '}'
$prefix{'endnotes'} = Cast to String '\endnote{'
$suffix{'endnotes'} = Cast to String '}'

# $prefix{'footnotes'} = Cast to String '{{'
# $suffix{'footnotes'} = Cast to String '}}'
# $prefix{'endnotes'} = Cast to String '(('
# $suffix{'endnotes'} = Cast to String '))'

$doc = Document.active
$notes = $doc.allNotes

if ! $notes.count
	exit 'No note found, exit...'
end

$startTime = Date.now
$text = $doc.text.copy

$totalNumberOfNotes = Hash.new 'footnotes', $doc.footnotes.count, 'endnotes', $doc.allNotes.count - $doc.footnotes.count

$errorTemplate = 'Default Note Text not removed for '


$lenPrev = $text.length
$defaultNT{'footnotes'} = $defaultNT{'endnotes'} = $defaultNT{'sectionnotes'} = '\A\Q'
$note = Note.insertFootnoteInTextAtIndex $text, $text.length
$defaultNT{'footnotes'} &= $note.contentSubtext & '\E'
$note = Note.insertEndnoteInTextAtIndex $text, $text.length
$defaultNT{'endnotes'} = $defaultNT{'sectionnotes'} &= $note.contentSubtext  & '\E'
$delRange = Range.newWithLocationAndBound $lenPrev, $text.length
$text.deleteInRange $delRange

$errors = Array.new
$placementToType = Hash.new 'page', 'footnotes', 'section', 'endnotes', 'document', 'endnotes'

$i = $text.length - 1
while $i
	$note = $text.noteAtIndex $i
	if Defined $note
		$type = $placementToType{$note.placement}
		$content = $note.contentSubtext
		$replaced = $content.findAndReplace $defaultNT{$type}, '', 'E'
		if $replaced == undefined
			$error = $errorTemplate.textByAppending $type, "\xA0(", $totalNumberOfNotes{$type} - $count{$type}, ')'
			$errors.prependValue $error
		end
		$count{$type} += 1
		$content = $prefix{$type}.textByAppending $content, $suffix{$type}
		$sel = TextSelection.newWithLocationAndLength $text, $i, 1
		$text.replaceInRange $sel.range, $content
		$i = $sel.location
	end
	$i -= 1
end

Document.newWithText $text

$finishTime = Date.now
$seconds = $finishTime.secondsSinceUnixEpoch - $startTime.secondsSinceUnixEpoch
$message = "Finished in $seconds s:\n"

foreach $type in $count.keys
	$message &= "\n" & $count{$type}
	$message &= " $type"
end
if $errors.count
	$errors.insertValueAtIndex "______\n", 0
	$message &= "\n" & $errors.join("\n")
end

exit $message

### end of macro ###

ATOM RSS1 RSS2