1 /** 2 * Temple (C) Dylan Knutson, 2013, distributed under the: 3 * Boost Software License - Version 1.0 - August 17th, 2003 4 * 5 * Permission is hereby granted, free of charge, to any person or organization 6 * obtaining a copy of the software and accompanying documentation covered by 7 * this license (the "Software") to use, reproduce, display, distribute, 8 * execute, and transmit the Software, and to prepare derivative works of the 9 * Software, and to permit third-parties to whom the Software is furnished to 10 * do so, all subject to the following: 11 * 12 * The copyright notices in the Software and this entire statement, including 13 * the above license grant, this restriction and the following disclaimer, 14 * must be included in all copies of the Software, in whole or in part, and 15 * all derivative works of the Software, unless such copies or derivative 16 * works are solely in the form of machine-executable object code generated by 17 * a source language processor. 18 */ 19 20 module temple.output_stream; 21 22 private { 23 import std.range; 24 import std.stdio; 25 } 26 27 // Wraps any generic output stream/sink 28 struct TempleOutputStream { 29 private: 30 //void delegate(string) scope_sink; 31 void delegate(string) sink; 32 33 public: 34 this(T)(ref T os) 35 if(isOutputRange!(T, string)) { 36 this.sink = delegate(str) { 37 os.put(str); 38 }; 39 } 40 41 this(ref File f) { 42 this.sink = delegate(str) { 43 f.write(str); 44 }; 45 } 46 47 this(void delegate(string) s) { 48 this.sink = s; 49 } 50 51 this(void function(string) s) { 52 this.sink = delegate(str) { 53 s(str); 54 }; 55 } 56 57 void put(string s) { 58 this.sink(s); 59 } 60 61 // for vibe.d's html escape 62 package 63 void put(dchar d) { 64 import std.conv; 65 this.sink(d.to!string); 66 } 67 68 invariant() { 69 assert(this.sink !is null); 70 } 71 } 72 73 // newtype struct 74 struct TempleInputStream { 75 // when called, 'into' pipes its output into OutputStream 76 void delegate(ref TempleOutputStream os) into; 77 78 invariant() { 79 assert(this.into !is null); 80 } 81 }