]> gitweb.factorcode.org Git - factor.git/commitdiff
Splitting up basis/wrap into three vocabs
authorDaniel Ehrenberg <littledan@Macintosh-103.local>
Mon, 9 Feb 2009 07:12:32 +0000 (01:12 -0600)
committerDaniel Ehrenberg <littledan@Macintosh-103.local>
Mon, 9 Feb 2009 07:12:32 +0000 (01:12 -0600)
basis/wrap/strings/strings-docs.factor [new file with mode: 0644]
basis/wrap/strings/strings-tests.factor [new file with mode: 0644]
basis/wrap/strings/strings.factor [new file with mode: 0644]
basis/wrap/words/words-docs.factor [new file with mode: 0644]
basis/wrap/words/words-tests.factor [new file with mode: 0644]
basis/wrap/words/words.factor [new file with mode: 0644]
basis/wrap/wrap-docs.factor
basis/wrap/wrap-tests.factor [deleted file]
basis/wrap/wrap.factor
basis/xml/writer/writer.factor

diff --git a/basis/wrap/strings/strings-docs.factor b/basis/wrap/strings/strings-docs.factor
new file mode 100644 (file)
index 0000000..e20780d
--- /dev/null
@@ -0,0 +1,25 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: help.syntax help.markup strings math ;
+IN: wrap.strings
+
+ABOUT: "wrap.strings"
+
+ARTICLE: "wrap.strings" "String word wrapping"
+"The " { $vocab-link "wrap.strings" } " vocabulary implements word wrapping for simple strings, assumed to be in monospace font."
+{ $subsection wrap-lines }
+{ $subsection wrap-string }
+{ $subsection wrap-indented-string } ;
+
+HELP: wrap-lines
+{ $values { "lines" string } { "width" integer } { "newlines" "sequence of strings" } }
+{ $description "Given a string, divides it into a sequence of lines where each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ;
+
+HELP: wrap-string
+{ $values { "string" string } { "width" integer } { "newstring" string } }
+{ $description "Given a string, alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ;
+
+HELP: wrap-indented-string
+{ $values { "string" string } { "width" integer } { "indent" string } { "newstring" string } }
+{ $description "Given a string, alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space. Before each line, the indent string is added." } ;
+
diff --git a/basis/wrap/strings/strings-tests.factor b/basis/wrap/strings/strings-tests.factor
new file mode 100644 (file)
index 0000000..0bea9b5
--- /dev/null
@@ -0,0 +1,41 @@
+! Copyright (C) 2008, 2009 Daniel Ehrenberg, Slava Pestov
+! See http://factorcode.org/license.txt for BSD license.
+USING: wrap.strings tools.test multiline ;
+IN: wrap.strings.tests
+
+[
+    <" This is a
+long piece
+of text
+that we
+wish to
+word wrap.">
+] [
+    <" This is a long piece of text that we wish to word wrap."> 10
+    wrap-string
+] unit-test
+    
+[
+    <"   This is a
+  long piece
+  of text
+  that we
+  wish to
+  word wrap.">
+] [
+    <" This is a long piece of text that we wish to word wrap."> 12
+    "  " wrap-indented-string
+] unit-test
+
+[ "this text\nhas lots\nof spaces" ]
+[ "this text        has lots of       spaces" 12 wrap-string ] unit-test
+
+[ "hello\nhow\nare\nyou\ntoday?" ]
+[ "hello how are you today?" 3 wrap-string ] unit-test
+
+[ "aaa\nbb cc\nddddd" ] [ "aaa bb cc ddddd" 6 wrap-string ] unit-test
+[ "aaa\nbb ccc\ndddddd" ] [ "aaa bb ccc dddddd" 6 wrap-string ] unit-test
+[ "aaa bb\ncccc\nddddd" ] [ "aaa bb cccc ddddd" 6 wrap-string ] unit-test
+[ "aaa bb\nccccccc\nddddddd" ] [ "aaa bb ccccccc ddddddd" 6 wrap-string ] unit-test
+
+\ wrap-string must-infer
diff --git a/basis/wrap/strings/strings.factor b/basis/wrap/strings/strings.factor
new file mode 100644 (file)
index 0000000..7009352
--- /dev/null
@@ -0,0 +1,29 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: wrap kernel sequences fry splitting math ;
+IN: wrap.strings
+
+<PRIVATE
+
+: split-lines ( string -- elements-lines )
+    string-lines [
+        " \t" split harvest
+        [ dup length 1 <element> ] map
+    ] map ;
+
+: join-elements ( wrapped-lines -- lines )
+    [ " " join ] map ;
+
+: join-lines ( strings -- string )
+    "\n" join ;
+
+PRIVATE>
+
+: wrap-lines ( lines width -- newlines )
+    [ split-lines ] dip '[ _ dup wrap join-elements ] map concat ;
+
+: wrap-string ( string width -- newstring )
+    wrap-lines join-lines ;
+
+: wrap-indented-string ( string width indent -- newstring )
+    [ length - wrap-lines ] keep '[ _ prepend ] map join-lines ;
diff --git a/basis/wrap/words/words-docs.factor b/basis/wrap/words/words-docs.factor
new file mode 100644 (file)
index 0000000..422aea0
--- /dev/null
@@ -0,0 +1,25 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: help.syntax help.markup math kernel ;
+IN: wrap.words
+
+ABOUT: "wrap.words"
+
+ARTICLE: "wrap.words" "Word object wrapping"
+"The " { $vocab-link "wrap.words" } " vocabulary implements word wrapping on abstract word objects, which have certain properties making it a more suitable input representation than strings."
+{ $subsection wrap-words }
+{ $subsection word }
+{ $subsection <word> } ;
+
+HELP: wrap-words
+{ $values { "words" { "a sequence of " { $instance word } "s" } } { "line-max" integer } { "line-ideal" integer } { "lines" "a sequence of sequences of words" } }
+{ $description "Divides the words into lines, where the sum of the lengths of the words on a line (not counting breaks at the end of the line) is at most the given maximum. The returned set of lines is optimized to minimize the square of the deviation of each line from the ideal width. It is not guaranteed to be the minimal number of lines. Every line except for the first one starts with a non-break, and every one but the last ends with a break." } ;
+
+HELP: word
+{ $class-description "A word is a Factor object annotated with a length (in the " { $snippet "width" } " slot) and knowledge about whether it is an allowable position for an optional line break (in the " { $snippet "break?" } " slot). Words can be created with " { $link <word> } "." }
+{ $see-also wrap-words } ;
+
+HELP: <word>
+{ $values { "key" object } { "width" integer } { "break?" { { $link t } " or " { $link POSTPONE: f } } } { "word" word } }
+{ $description "Creates a " { $link word } " object with the given parameters." }
+{ $see-also wrap-words } ;
diff --git a/basis/wrap/words/words-tests.factor b/basis/wrap/words/words-tests.factor
new file mode 100644 (file)
index 0000000..7598b38
--- /dev/null
@@ -0,0 +1,82 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test wrap.words sequences ;
+IN: wrap.words.tests    
+
+[
+    {
+        {
+            T{ word f 1 10 f }
+            T{ word f 2 10 f }
+            T{ word f 3 2 t }
+        }
+        {
+            T{ word f 4 10 f }
+            T{ word f 5 10 f }
+        }
+    }
+] [
+    {
+        T{ word f 1 10 f }
+        T{ word f 2 10 f }
+        T{ word f 3 2 t }
+        T{ word f 4 10 f }
+        T{ word f 5 10 f }
+    } 35 35 wrap-words [ { } like ] map
+] unit-test
+
+[
+    {
+        {
+            T{ word f 1 10 f }
+            T{ word f 2 10 f }
+            T{ word f 3 9 t }
+            T{ word f 3 9 t }
+            T{ word f 3 9 t }
+        }
+        {
+            T{ word f 4 10 f }
+            T{ word f 5 10 f }
+        }
+    }
+] [
+    {
+        T{ word f 1 10 f }
+        T{ word f 2 10 f }
+        T{ word f 3 9 t }
+        T{ word f 3 9 t }
+        T{ word f 3 9 t }
+        T{ word f 4 10 f }
+        T{ word f 5 10 f }
+    } 35 35 wrap-words [ { } like ] map
+] unit-test
+
+[
+    {
+        {
+            T{ word f 1 10 t }
+            T{ word f 1 10 f }
+            T{ word f 3 9 t }
+        }
+        {
+            T{ word f 2 10 f }
+            T{ word f 3 9 t }
+        }
+        {
+            T{ word f 4 10 f }
+            T{ word f 5 10 f }
+        }
+    }
+] [
+    {
+        T{ word f 1 10 t }
+        T{ word f 1 10 f }
+        T{ word f 3 9 t }
+        T{ word f 2 10 f }
+        T{ word f 3 9 t }
+        T{ word f 4 10 f }
+        T{ word f 5 10 f }
+    } 35 35 wrap-words [ { } like ] map
+] unit-test
+
+\ wrap-words must-infer
diff --git a/basis/wrap/words/words.factor b/basis/wrap/words/words.factor
new file mode 100644 (file)
index 0000000..00f257a
--- /dev/null
@@ -0,0 +1,40 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: sequences kernel splitting.monotonic accessors wrap grouping ;
+IN: wrap.words
+
+TUPLE: word key width break? ;
+C: <word> word
+
+<PRIVATE
+
+: words-length ( words -- length )
+    [ width>> ] map sum ;
+
+: make-element ( whites blacks -- element )
+    [ append ] [ [ words-length ] bi@ ] 2bi <element> ;
+: ?first2 ( seq -- first/f second/f )
+    [ 0 swap ?nth ]
+    [ 1 swap ?nth ] bi ;
+
+: split-words ( seq -- half-elements )
+    [ [ break?>> ] bi@ = ] monotonic-split ;
+
+: ?first-break ( seq -- newseq f/element )
+    dup first first break?>>
+    [ unclip-slice f swap make-element ]
+    [ f ] if ;
+
+: make-elements ( seq f/element -- elements )
+    [ 2 <groups> [ ?first2 make-element ] map ] dip
+    [ prefix ] when* ;
+
+: words>elements ( seq -- newseq )
+    split-words ?first-break make-elements ;
+
+PRIVATE>
+
+: wrap-words ( words line-max line-ideal -- lines )
+    [ words>elements ] 2dip wrap [ concat ] map ;
+
index 59c0352bc740cc6d000883dfec8662d61173e2fa..feac7c51a790e533b38c4c0babf4d0d944e73a93 100644 (file)
@@ -6,36 +6,6 @@ IN: wrap
 ABOUT: "wrap"
 
 ARTICLE: "wrap" "Word wrapping"
-"The " { $vocab-link "wrap" } " vocabulary implements word wrapping. There is support for simple string wrapping, with the following words:"
-{ $subsection wrap-lines }
-{ $subsection wrap-string }
-{ $subsection wrap-indented-string }
-"Additionally, the vocabulary provides capabilities to wrap arbitrary groups of things, in units called segments."
-{ $subsection wrap-segments }
-{ $subsection segment }
-{ $subsection <segment> } ;
-
-HELP: wrap-lines
-{ $values { "lines" string } { "width" integer } { "newlines" "sequence of strings" } }
-{ $description "Given a string, divides it into a sequence of lines where each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ;
-
-HELP: wrap-string
-{ $values { "string" string } { "width" integer } { "newstring" string } }
-{ $description "Given a string, alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space." } ;
-
-HELP: wrap-indented-string
-{ $values { "string" string } { "width" integer } { "indent" string } { "newstring" string } }
-{ $description "Given a string, alters the whitespace in the string so that each line has no more than " { $snippet "width" } " characters, unless there is a word longer than " { $snippet "width" } ". Linear whitespace between words is converted to a single space. Before each line, the indent string is added." } ;
-
-HELP: wrap-segments
-{ $values { "segments" { "a sequence of " { $instance segment } "s" } } { "line-max" integer } { "line-ideal" integer } { "lines" "a sequence of sequences of words" } }
-{ $description "Divides the words into lines, where the sum of the lengths of the words on a line (not counting breaks at the end of the line) is at most the given maximum. The returned set of lines is optimized to minimize the square of the deviation of each line from the ideal width. It is not guaranteed to be the minimal number of lines. Every line except for the first one starts with a non-break, and every one but the last ends with a break." } ;
-
-HELP: segment
-{ $class-description "A segment is a Factor object annotated with a length (in the " { $snippet "width" } " slot) and knowledge about whether it is an allowable position for an optional line break (in the " { $snippet "break?" } " slot). Elements can be created with " { $link <segment> } "." }
-{ $see-also wrap-segments } ;
-
-HELP: <segment>
-{ $values { "key" object } { "width" integer } { "break?" { { $link t } " or " { $link POSTPONE: f } } } { "segment" segment } }
-{ $description "Creates a " { $link segment } " object with the given parameters." }
-{ $see-also wrap-segments } ;
+"The " { $vocab-link "wrap" } " vocabulary implements word wrapping. Wrapping can take place based on simple strings, assumed to be monospace, or abstract word objects."
+{ $vocab-subsection "String word wrapping" "wrap.strings" } 
+{ $vocab-subsection "Word object wrapping" "wrap.words" } ;
diff --git a/basis/wrap/wrap-tests.factor b/basis/wrap/wrap-tests.factor
deleted file mode 100644 (file)
index eeea385..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-! Copyright (C) 2008, 2009 Daniel Ehrenberg, Slava Pestov
-! See http://factorcode.org/license.txt for BSD license.
-USING: tools.test wrap multiline sequences ;
-IN: wrap.tests
-    
-[
-    {
-        {
-            T{ segment f 1 10 f }
-            T{ segment f 2 10 f }
-            T{ segment f 3 2 t }
-        }
-        {
-            T{ segment f 4 10 f }
-            T{ segment f 5 10 f }
-        }
-    }
-] [
-    {
-        T{ segment f 1 10 f }
-        T{ segment f 2 10 f }
-        T{ segment f 3 2 t }
-        T{ segment f 4 10 f }
-        T{ segment f 5 10 f }
-    } 35 35 wrap-segments [ { } like ] map
-] unit-test
-
-[
-    {
-        {
-            T{ segment f 1 10 f }
-            T{ segment f 2 10 f }
-            T{ segment f 3 9 t }
-            T{ segment f 3 9 t }
-            T{ segment f 3 9 t }
-        }
-        {
-            T{ segment f 4 10 f }
-            T{ segment f 5 10 f }
-        }
-    }
-] [
-    {
-        T{ segment f 1 10 f }
-        T{ segment f 2 10 f }
-        T{ segment f 3 9 t }
-        T{ segment f 3 9 t }
-        T{ segment f 3 9 t }
-        T{ segment f 4 10 f }
-        T{ segment f 5 10 f }
-    } 35 35 wrap-segments [ { } like ] map
-] unit-test
-
-[
-    {
-        {
-            T{ segment f 1 10 t }
-            T{ segment f 1 10 f }
-            T{ segment f 3 9 t }
-        }
-        {
-            T{ segment f 2 10 f }
-            T{ segment f 3 9 t }
-        }
-        {
-            T{ segment f 4 10 f }
-            T{ segment f 5 10 f }
-        }
-    }
-] [
-    {
-        T{ segment f 1 10 t }
-        T{ segment f 1 10 f }
-        T{ segment f 3 9 t }
-        T{ segment f 2 10 f }
-        T{ segment f 3 9 t }
-        T{ segment f 4 10 f }
-        T{ segment f 5 10 f }
-    } 35 35 wrap-segments [ { } like ] map
-] unit-test
-
-[
-    <" This is a
-long piece
-of text
-that we
-wish to
-word wrap.">
-] [
-    <" This is a long piece of text that we wish to word wrap."> 10
-    wrap-string
-] unit-test
-    
-[
-    <"   This is a
-  long piece
-  of text
-  that we
-  wish to
-  word wrap.">
-] [
-    <" This is a long piece of text that we wish to word wrap."> 12
-    "  " wrap-indented-string
-] unit-test
-
-[ "this text\nhas lots\nof spaces" ]
-[ "this text        has lots of       spaces" 12 wrap-string ] unit-test
-
-[ "hello\nhow\nare\nyou\ntoday?" ]
-[ "hello how are you today?" 3 wrap-string ] unit-test
-
-[ "aaa\nbb cc\nddddd" ] [ "aaa bb cc ddddd" 6 wrap-string ] unit-test
-[ "aaa\nbb ccc\ndddddd" ] [ "aaa bb ccc dddddd" 6 wrap-string ] unit-test
-[ "aaa bb\ncccc\nddddd" ] [ "aaa bb cccc ddddd" 6 wrap-string ] unit-test
-[ "aaa bb\nccccccc\nddddddd" ] [ "aaa bb ccccccc ddddddd" 6 wrap-string ] unit-test
-
-\ wrap-string must-infer
-\ wrap-segments must-infer
index f54c858bf4d611d7a6945b36dd6a91905f982528..55fe10283ac1957a9274a9756f6cce286e1ea9d6 100644 (file)
@@ -1,10 +1,10 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
 USING: kernel sequences math arrays locals fry accessors
 lists splitting call make combinators.short-circuit namespaces
 grouping splitting.monotonic ;
 IN: wrap
 
-<PRIVATE
-
 ! black is the text length, white is the whitespace length
 TUPLE: element contents black white ;
 C: <element> element
@@ -93,65 +93,3 @@ SYMBOL: line-ideal
         min-cost
         post-process
     ] with-scope ;
-
-PRIVATE>
-
-TUPLE: segment key width break? ;
-C: <segment> segment
-
-<PRIVATE
-
-: segments-length ( segments -- length )
-    [ width>> ] map sum ;
-
-: make-element ( whites blacks -- element )
-    [ append ] [ [ segments-length ] bi@ ] 2bi <element> ;
-: ?first2 ( seq -- first/f second/f )
-    [ 0 swap ?nth ]
-    [ 1 swap ?nth ] bi ;
-
-: split-segments ( seq -- half-elements )
-    [ [ break?>> ] bi@ = ] monotonic-split ;
-
-: ?first-break ( seq -- newseq f/element )
-    dup first first break?>>
-    [ unclip-slice f swap make-element ]
-    [ f ] if ;
-
-: make-elements ( seq f/element -- elements )
-    [ 2 <groups> [ ?first2 make-element ] map ] dip
-    [ prefix ] when* ;
-
-: segments>elements ( seq -- newseq )
-    split-segments ?first-break make-elements ;
-
-PRIVATE>
-
-: wrap-segments ( segments line-max line-ideal -- lines )
-    [ segments>elements ] 2dip wrap [ concat ] map ;
-
-<PRIVATE
-
-: split-lines ( string -- elements-lines )
-    string-lines [
-        " \t" split harvest
-        [ dup length 1 <element> ] map
-    ] map ;
-
-: join-elements ( wrapped-lines -- lines )
-    [ " " join ] map ;
-
-: join-lines ( strings -- string )
-    "\n" join ;
-
-PRIVATE>
-
-: wrap-lines ( lines width -- newlines )
-    [ split-lines ] dip '[ _ dup wrap join-elements ] map concat ;
-
-: wrap-string ( string width -- newstring )
-    wrap-lines join-lines ;
-
-: wrap-indented-string ( string width indent -- newstring )
-    [ length - wrap-lines ] keep '[ _ prepend ] map join-lines ;
index 4b80e0818ef6b89c27abba0795b80fdd986fdec7..4f5bad1aa58054b97f371ae74af2820cecb45181 100755 (executable)
@@ -2,7 +2,7 @@
 ! See http://factorcode.org/license.txt for BSD license.\r
 USING: hashtables kernel math namespaces sequences strings\r
 assocs combinators io io.streams.string accessors\r
-xml.data wrap xml.entities unicode.categories fry ;\r
+xml.data wrap.strings xml.entities unicode.categories fry ;\r
 IN: xml.writer\r
 \r
 SYMBOL: sensitive-tags\r