Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
34
Task/Word-wrap/AppleScript/word-wrap-1.applescript
Normal file
34
Task/Word-wrap/AppleScript/word-wrap-1.applescript
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
on wrapParagraph(para, lineWidth)
|
||||
if (para is "") then return para
|
||||
set astid to AppleScript's text item delimiters
|
||||
set AppleScript's text item delimiters to {space, tab} -- Doesn't include character id 160 (NO-BREAK SPACE).
|
||||
script o
|
||||
property wrds : para's text items -- Space- or tab-delimited chunks.
|
||||
end script
|
||||
|
||||
set spaceWidth to (count space) -- ;-)
|
||||
set spaceLeft to lineWidth
|
||||
set theLines to {}
|
||||
set i to 1
|
||||
repeat with j from 1 to (count o's wrds)
|
||||
set wordWidth to (count item j of o's wrds)
|
||||
if (wordWidth + spaceWidth > spaceLeft) then
|
||||
set end of theLines to text 1 thru (-1 - wordWidth) of (text from text item i to text item j of para)
|
||||
set i to j
|
||||
set spaceLeft to lineWidth - wordWidth
|
||||
else
|
||||
set spaceLeft to spaceLeft - (wordWidth + spaceWidth)
|
||||
end if
|
||||
end repeat
|
||||
set end of theLines to text from text item i to end of para
|
||||
|
||||
set AppleScript's text item delimiters to character id 8232 -- U+2028 (LINE SEPARATOR).
|
||||
set output to theLines as text
|
||||
set AppleScript's text item delimiters to astid
|
||||
|
||||
return output
|
||||
end wrapParagraph
|
||||
|
||||
local para
|
||||
set para to "If there is a way to do this that is built-in, trivial, or provided in a standard library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia."
|
||||
return wrapParagraph(para, 70) & (linefeed & linefeed) & wrapParagraph(para, 40)
|
||||
9
Task/Word-wrap/AppleScript/word-wrap-2.applescript
Normal file
9
Task/Word-wrap/AppleScript/word-wrap-2.applescript
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
"If there is a way to do this that is built-in, trivial, or provided
|
||||
in a standard library, show that. Otherwise implement the minimum
|
||||
length greedy algorithm from Wikipedia.
|
||||
|
||||
If there is a way to do this that is
|
||||
built-in, trivial, or provided in a
|
||||
standard library, show that. Otherwise
|
||||
implement the minimum length greedy
|
||||
algorithm from Wikipedia."
|
||||
32
Task/Word-wrap/AppleScript/word-wrap-3.applescript
Normal file
32
Task/Word-wrap/AppleScript/word-wrap-3.applescript
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
on wrapParagraph(para, lineWidth)
|
||||
set theLines to {}
|
||||
set spaceTab to space & tab
|
||||
set len to (count para)
|
||||
set i to 1
|
||||
repeat until (i > len)
|
||||
set j to i + lineWidth - 1
|
||||
if (j < len) then
|
||||
repeat with j from j to i by -1
|
||||
if (character j of para is in spaceTab) then exit repeat
|
||||
end repeat
|
||||
-- The "greedy" algorithm keeps words which are longer than or
|
||||
-- the same length as the line width intact. Do the same here.
|
||||
if (j = i) then
|
||||
repeat with j from (i + lineWidth) to len
|
||||
if (character j of para is in spaceTab) then exit repeat
|
||||
end repeat
|
||||
end if
|
||||
else
|
||||
set j to len
|
||||
end if
|
||||
set end of theLines to text i thru j of para
|
||||
set i to j + 1
|
||||
end repeat
|
||||
|
||||
set astid to AppleScript's text item delimiters
|
||||
set AppleScript's text item delimiters to character id 8232 -- U+2028 (LINE SEPARATOR).
|
||||
set output to theLines as text
|
||||
set AppleScript's text item delimiters to astid
|
||||
|
||||
return output
|
||||
end wrapParagraph
|
||||
16
Task/Word-wrap/AppleScript/word-wrap-4.applescript
Normal file
16
Task/Word-wrap/AppleScript/word-wrap-4.applescript
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
|
||||
use framework "Foundation"
|
||||
|
||||
on wrapParagraph(para, lineWidth)
|
||||
if (para is "") then return para
|
||||
set str to current application's class "NSMutableString"'s stringWithString:(para)
|
||||
-- Replace each run of up to (lineWidth - 1) characters followed by a space or a tab,
|
||||
-- or by the end of the paragraph, with itself and a LINE SEPARATOR character.
|
||||
tell str to replaceOccurrencesOfString:(".{1," & (lineWidth - 1) & "}(?:[ \\t]|\\Z)") withString:("$0" & character id 8232) ¬
|
||||
options:(current application's NSRegularExpressionSearch) range:({0, its |length|()})
|
||||
-- Remove the LINE SEPARATOR inserted at the end.
|
||||
tell str to replaceOccurrencesOfString:(character id 8232) withString:("") ¬
|
||||
options:(0) range:({(its |length|()) - 2, 2})
|
||||
|
||||
return str as text
|
||||
end wrapParagraph
|
||||
Loading…
Add table
Add a link
Reference in a new issue