Quoted printable vocab

db4
Daniel Ehrenberg 2009-01-28 12:29:25 -06:00
parent b13ebfe757
commit 371b919abc
7 changed files with 125 additions and 0 deletions

2
basis/base64/tags.txt Normal file
View File

@ -0,0 +1,2 @@
parsing
web

View File

@ -0,0 +1 @@
Daniel Ehrenberg

View File

@ -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." } ;

View File

@ -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

View File

@ -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 ;

View File

@ -0,0 +1 @@
Quoted printable encoding/decoding

View File

@ -0,0 +1,2 @@
parsing
web