on master help sign in
Change document_render to be capable of rendering any node
b18669fNeil Mitchell
cbits/hexml.c+5−2
@@ -149,14 +149,17 @@ void render_tag(render* r, node* n)
149 render_char(r, '>');
149 render_char(r, '>');
150}
150}
151
151
152−int document_render(document* d, char* buffer, int length)
152+int node_render(document* d, node* n, char* buffer, int length)
153{
153{
154 render r;
154 render r;
155 r.d = d;
155 r.d = d;
156 r.buffer = buffer;
156 r.buffer = buffer;
157 r.length = length;
157 r.length = length;
158 r.cursor = 0;
158 r.cursor = 0;
159+ if (n->name.length == 0)
159− render_content(&r, document_node(d));
160+ render_content(&r, n);
161+ else
162+ render_tag(&r, n);
160 return r.cursor;
163 return r.cursor;
161}
164}
162
165
cbits/hexml.h+2−2
@@ -34,9 +34,9 @@ document* document_parse(char* s, int slen);
34// Free the memory returned from document_parse.
34// Free the memory returned from document_parse.
35void document_free(document* d);
35void document_free(document* d);
36
36
37−// generate a fresh document with the same semantics
37+// generate a fresh string with the same semantics as the node
38// requires an input buffer, returns the size of the rendered document
38// requires an input buffer, returns the size of the rendered document
39−int document_render(document* d, char* buffer, int length);
39+int node_render(document* d, node* n, char* buffer, int length);
40
40
41// return either NULL (successful parse) or the error message
41// return either NULL (successful parse) or the error message
42char* document_error(document* d);
42char* document_error(document* d);
src/Text/XML/Hexml.hs+4−3
@@ -48,7 +48,7 @@ instance Storable Str where
48foreign import ccall document_parse :: CString -> CInt -> IO (Ptr CDocument)
48foreign import ccall document_parse :: CString -> CInt -> IO (Ptr CDocument)
49foreign import ccall document_free :: Ptr CDocument -> IO ()
49foreign import ccall document_free :: Ptr CDocument -> IO ()
50foreign import ccall "&document_free" document_free_funptr :: FunPtr (Ptr CDocument -> IO ())
50foreign import ccall "&document_free" document_free_funptr :: FunPtr (Ptr CDocument -> IO ())
51−foreign import ccall document_render :: Ptr CDocument -> CString -> CInt -> IO CInt
51+foreign import ccall node_render :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO CInt
52foreign import ccall document_error :: Ptr CDocument -> IO CString
52foreign import ccall document_error :: Ptr CDocument -> IO CString
53foreign import ccall document_node :: Ptr CDocument -> IO (Ptr CNode)
53foreign import ccall document_node :: Ptr CDocument -> IO (Ptr CNode)
54
54
@@ -86,8 +86,9 @@ documentParse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.single
86
86
87documentRender :: Document -> BS.ByteString
87documentRender :: Document -> BS.ByteString
88documentRender (Document _ doc) = unsafePerformIO $ withForeignPtr doc $ \d -> do
88documentRender (Document _ doc) = unsafePerformIO $ withForeignPtr doc $ \d -> do
89+ n <- document_node d
89− i <- document_render d nullPtr 0
90+ i <- node_render d n nullPtr 0
90− BS.create (fromIntegral i) $ \ptr -> void $ document_render d (castPtr ptr) i
91+ BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
91
92
92
93
93documentNode :: Document -> Node
94documentNode :: Document -> Node