! Copyright (C) 2014 John Benediktsson ! See http://factorcode.org/license.txt for BSD license USING: accessors byte-arrays colors.constants combinators formatting fry images images.loader images.loader.private images.viewer io io.encodings.binary io.encodings.string io.encodings.utf8 io.sockets io.styles kernel make math math.parser namespaces present prettyprint sequences splitting summary urls urls.encoding vocabs ; IN: gopher url present ; : ( item -- gopher-link ) [ "" ] [ unclip swap "\t" split first4 gopher-link boa ] if-empty ; M: gopher-link >url dup type>> CHAR: h = [ selector>> "URL:" ?head drop ] [ { [ host>> ] [ port>> ] [ type>> ] [ selector>> ] } cleave "gopher://%s:%s/%s%s" sprintf ] if >url ; : get-menu ( selector -- lines ) get-text [ ] map ; : get-selector ( selector -- stuff ) "/" split1 "" or swap dup length 1 > [ string>number ] [ first ] if { { A_TEXT [ get-text ] } { A_MENU [ get-menu ] } { A_INDEX [ get-menu ] } { A_GIF [ get-gif ] } [ drop get-binary ] } case ; PRIVATE> ERROR: not-a-gopher-url url ; : gopher ( url -- object ) dup url? [ >url ] unless dup protocol>> "gopher" = [ not-a-gopher-url ] unless { [ host>> ] [ port>> 70 or binary ] [ path>> rest ] [ query>> assoc>query url-decode "?" glue ] } cleave '[ _ get-selector ] with-client ; : gopher. ( url -- ) gopher { { [ dup byte-array? ] [ . ] } { [ dup image? ] [ image. ] } [ [ dup gopher-link? [ dup type>> CHAR: i = [ name>> print ] [ [ name>> ] keep [ presented ,, COLOR: blue foreground ,, ] H{ } make format nl ] if ] [ print ] if ] each ] } cond ;