]> gitweb.factorcode.org Git - factor.git/commitdiff
Quoted printable vocab
authorDaniel Ehrenberg <littledan@Macintosh-103.local>
Wed, 28 Jan 2009 18:29:25 +0000 (12:29 -0600)
committerDaniel Ehrenberg <littledan@Macintosh-103.local>
Wed, 28 Jan 2009 18:29:25 +0000 (12:29 -0600)
basis/base64/tags.txt [new file with mode: 0644]
basis/quoted-printable/authors.txt [new file with mode: 0644]
basis/quoted-printable/quoted-printable-docs.factor [new file with mode: 0644]
basis/quoted-printable/quoted-printable-tests.factor [new file with mode: 0644]
basis/quoted-printable/quoted-printable.factor [new file with mode: 0644]
basis/quoted-printable/summary.txt [new file with mode: 0644]
basis/quoted-printable/tags.txt [new file with mode: 0644]

diff --git a/basis/base64/tags.txt b/basis/base64/tags.txt
new file mode 100644 (file)
index 0000000..8fd3ecc
--- /dev/null
@@ -0,0 +1,2 @@
+parsing
+web
diff --git a/basis/quoted-printable/authors.txt b/basis/quoted-printable/authors.txt
new file mode 100644 (file)
index 0000000..f990dd0
--- /dev/null
@@ -0,0 +1 @@
+Daniel Ehrenberg
diff --git a/basis/quoted-printable/quoted-printable-docs.factor b/basis/quoted-printable/quoted-printable-docs.factor
new file mode 100644 (file)
index 0000000..81219a3
--- /dev/null
@@ -0,0 +1,27 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: help.markup help.syntax strings byte-arrays io.encodings.string ;
+IN: quoted-printable
+
+ABOUT: "quoted-printable"
+
+ARTICLE: "quoted-printable" "Quoted printable encoding"
+"The " { $vocab-link "quoted-printable" } " vocabulary implements RFC 2045 part 6.7, providing words for reading and generating quotable printed text."
+{ $subsection >quoted }
+{ $subsection >quoted-lines }
+{ $subsection quoted> } ;
+
+HELP: >quoted
+{ $values { "byte-array" byte-array } { "string" string } }
+{ $description "Encodes a byte array as quoted printable, on a single line." }
+{ $warning "To encode a string in quoted printable, first use the " { $link encode } " word." } ;
+
+HELP: >quoted-lines
+{ $values { "byte-array" byte-array } { "string" string } }
+{ $description "Encodes a byte array as quoted printable, with soft line breaks inserted so the output lines are no longer than 76 characters." }
+{ $warning "To encode a string in quoted printable, first use the " { $link encode } " word with a specific encoding." } ;
+
+HELP: quoted>
+{ $values { "string" string } { "byte-array" byte-array } }
+{ $description "Decodes a quoted printable string into an array of the bytes represented." }
+{ $warning "When decoding something in quoted printable form and using it as a string, be sure to use the " { $link decode } " word rather than simply converting the byte array to a string." } ;
diff --git a/basis/quoted-printable/quoted-printable-tests.factor b/basis/quoted-printable/quoted-printable-tests.factor
new file mode 100644 (file)
index 0000000..6f42a48
--- /dev/null
@@ -0,0 +1,30 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: tools.test quoted-printable multiline io.encodings.string
+sequences io.encodings.8-bit splitting kernel ;
+IN: quoted-printable.tests
+
+[ <" José was the
+person who knew how to write the letters:
+    ő and ü 
+and we didn't know hów tö do thât"> ]
+[ <" Jos=E9 was the
+person who knew how to write the letters:
+    =F5 and =FC=20
+and w=
+e didn't know h=F3w t=F6 do th=E2t"> quoted> latin2 decode ] unit-test
+
+[ <" Jos=E9 was the=0Aperson who knew how to write the letters:=0A    =F5 and =FC=0Aand we didn't know h=F3w t=F6 do th=E2t"> ]
+[ <" José was the
+person who knew how to write the letters:
+    ő and ü
+and we didn't know hów tö do thât"> latin2 encode >quoted ] unit-test
+
+: message ( -- str )
+    55 [ "hello" ] replicate concat ;
+
+[ f ] [ message >quoted "=\r\n" swap subseq? ] unit-test
+[ 1 ] [ message >quoted string-lines length ] unit-test
+[ t ] [ message >quoted-lines "=\r\n" swap subseq? ] unit-test
+[ 4 ] [ message >quoted-lines string-lines length ] unit-test
+[ "===o" ] [ message >quoted-lines string-lines [ peek ] "" map-as ] unit-test
diff --git a/basis/quoted-printable/quoted-printable.factor b/basis/quoted-printable/quoted-printable.factor
new file mode 100644 (file)
index 0000000..83fee52
--- /dev/null
@@ -0,0 +1,62 @@
+! Copyright (C) 2009 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: sequences strings kernel io.encodings.string
+math.order ascii math io io.encodings.utf8 io.streams.string
+combinators.short-circuit math.parser arrays ;
+IN: quoted-printable
+
+! This implements RFC 2045 section 6.7
+
+<PRIVATE
+
+: assure-small ( ch -- ch )
+    dup 256 <
+    [ "Cannot quote a character greater than 255" throw ] unless ;
+
+: printable? ( ch -- ? )
+    {
+        [ CHAR: \s CHAR: < between? ]
+        [ CHAR: > CHAR: ~ between? ]
+        [ CHAR: \t = ]
+    } 1|| ;
+
+: char>quoted ( ch -- str )
+    dup printable? [ 1string ] [
+        assure-small >hex >upper
+        2 CHAR: 0 pad-left 
+        CHAR: = prefix
+    ] if ;
+
+: take-some ( seqs -- seqs seq )
+    0 over [ length + dup 76 >= ] find drop nip
+    [ 1- cut-slice swap ] [ f swap ] if* concat ;
+
+: divide-lines ( strings -- strings )
+    [ dup ] [ take-some ] [ ] produce nip ;
+
+PRIVATE>
+
+: >quoted ( byte-array -- string )
+    [ char>quoted ] { } map-as concat "" like ;
+
+: >quoted-lines ( byte-array -- string )
+    [ char>quoted ] { } map-as
+    divide-lines "=\r\n" join ;
+
+<PRIVATE
+
+: read-char ( byte -- ch )
+    dup CHAR: = = [
+       drop read1 dup CHAR: \n =
+       [ drop read1 read-char ]
+       [ read1 2array hex> ] if
+    ] when ;
+
+: read-quoted ( -- bytes )
+    [ read1 dup ] [ read-char ] [ drop ] B{ } produce-as ;
+
+PRIVATE>
+
+: quoted> ( string -- byte-array )
+    ! Input should already be normalized to make \r\n into \n
+    [ read-quoted ] with-string-reader ;
diff --git a/basis/quoted-printable/summary.txt b/basis/quoted-printable/summary.txt
new file mode 100644 (file)
index 0000000..c32ac1f
--- /dev/null
@@ -0,0 +1 @@
+Quoted printable encoding/decoding
diff --git a/basis/quoted-printable/tags.txt b/basis/quoted-printable/tags.txt
new file mode 100644 (file)
index 0000000..8fd3ecc
--- /dev/null
@@ -0,0 +1,2 @@
+parsing
+web