![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Markdown to HTML - Streaming ModeSee more Markdown ExamplesShows how to convert Markdown to HTML in streaming mode, ideal for processing deltas with fragments of incoming Markdown during AI streaming responses. This example will simulate processing incoming AI delta's by streaming 80-byte chunks from a markdown file.Note: This example requires Chilkat v11.2.0 or greater.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) DECLARE @success int SELECT @success = 0 DECLARE @options int EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @options OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OAMethod @options, 'UpdateString', @success OUT, 'theme', 'raw' -- Indicate streaming mode EXEC sp_OAMethod @options, 'UpdateBool', @success OUT, 'streaming', 1 -- Load the full markdown file, which will be converted to HTML 80 bytes at a time in streaming mode. -- The sample markdown input for this example is identical to the one at Markdown to HTML - Full Document, Raw, where you can view it. DECLARE @sbFullMarkdown int EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbFullMarkdown OUT EXEC sp_OAMethod @sbFullMarkdown, 'LoadFile', @success OUT, 'qa_data/markdown/test1.md', 'utf-8' IF @success = 0 BEGIN EXEC sp_OAGetProperty @sbFullMarkdown, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @options EXEC @hr = sp_OADestroy @sbFullMarkdown RETURN END -- This will contain the fragment of HTMl produced at each step in streaming mode. DECLARE @sbHtmlFrag int EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbHtmlFrag OUT -- We'll accumulate the full HTML result here, by appending each HTLM fragment DECLARE @sbHtml int EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbHtml OUT -- This will contain the chunk of markdown not yet converted to HTML. DECLARE @sbStreamingMarkdown int EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbStreamingMarkdown OUT -- This contains the current 80 byte markdown chunk to be processed. -- The last chunk can be less than 80 bytes. DECLARE @sbMdChunk int EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMdChunk OUT -- Let's safeguard the loop to process a max of 160K of markdown (2000 x 80 bytes = 160K) -- This is to prevent an infinite loop in case something isn't working correctly. DECLARE @safety int SELECT @safety = 0 EXEC sp_OAGetProperty @sbFullMarkdown, 'Length', @iTmp0 OUT WHILE (@iTmp0 > 0) and (@safety < 2000) BEGIN DECLARE @chunkSize int SELECT @chunkSize = 80 EXEC sp_OAGetProperty @sbFullMarkdown, 'Length', @iTmp0 OUT IF @iTmp0 < @chunkSize BEGIN EXEC sp_OAGetProperty @sbFullMarkdown, 'Length', @chunkSize OUT END -- Remove the leading chunk from the sbFullMarkdown and append to sbStreamingMarkdown. DECLARE @s nvarchar(4000) EXEC sp_OAMethod @sbFullMarkdown, 'GetRange', @s OUT, 0, @chunkSize, 1 EXEC sp_OAMethod @sbStreamingMarkdown, 'Append', @success OUT, @s PRINT '-------------------------------------------------------------' EXEC sp_OAMethod @sbStreamingMarkdown, 'ToCRLF', @success OUT EXEC sp_OAMethod @sbStreamingMarkdown, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 -- Convert what is possible and append to the HTML fragment. -- It's possible that no additional HTML can be generated if the markdown chunk is does not contain even a single full line. -- In streaming mode, MarkdownToHtml appends any additional HTML it can generate to the StringBuilder passed in the 2nd argument. -- If we want to only see what new HTML was generated from this chunk, we need to clear the StringBuilder before calling MarkdownToHtml. -- The markdown that was converted is removed from the caller's contents. EXEC sp_OAMethod @sbHtmlFrag, 'Clear', NULL EXEC sp_OAMethod @sbStreamingMarkdown, 'MarkdownToHtml', @success OUT, @options, @sbHtmlFrag -- Accumulate the HTML fragments into a full HTML document. EXEC sp_OAMethod @sbHtml, 'AppendSb', @success OUT, @sbHtmlFrag PRINT '----' EXEC sp_OAMethod @sbHtmlFrag, 'ToCRLF', @success OUT EXEC sp_OAMethod @sbHtmlFrag, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 SELECT @safety = @safety + 1 END -- Flush any remaining closing HTML tags by passing a final line-ending. PRINT '----FINAL----------------------------------------------------' EXEC sp_OAMethod @sbStreamingMarkdown, 'Append', @success OUT, CHAR(10) EXEC sp_OAMethod @sbStreamingMarkdown, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 EXEC sp_OAMethod @sbHtmlFrag, 'Clear', NULL EXEC sp_OAMethod @sbStreamingMarkdown, 'MarkdownToHtml', @success OUT, @options, @sbHtmlFrag EXEC sp_OAMethod @sbHtml, 'AppendSb', @success OUT, @sbHtmlFrag PRINT '----' EXEC sp_OAMethod @sbHtmlFrag, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 PRINT '' PRINT '**** Full Accumulated HTML ****' EXEC sp_OAMethod @sbHtml, 'ToCRLF', @success OUT EXEC sp_OAMethod @sbHtml, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 -- Notice how the 1st loop iteration produces no HTML output. This is because there is not yet a complete line of markdown to process. -- Sample ouput: -- ------------------------------------------------------------- -- Here’s a simple implementation of the standard C library function `strncpy`, wri -- ---- -- -- ------------------------------------------------------------- -- Here’s a simple implementation of the standard C library function `strncpy`, written from scratch — demonstrating how it works internally: -- -- ```c -- #include <st -- ---- -- <p>Here’s a simple implementation of the standard C library function <code>strncpy</code>, written from scratch — demonstrating how it works internally:</p> -- -- ------------------------------------------------------------- -- #include <stddef.h> // for size_t -- -- char *my_strncpy(char *dest, const char *src, size_t n -- ---- -- <pre id="pre_dd232b5f-2279-4841-b602-5840072f7a84"><code id="dd232b5f-2279-4841-b602-5840072f7a84" class="language-c">#include <stddef.h> // for size_t -- -- -- ------------------------------------------------------------- -- char *my_strncpy(char *dest, const char *src, size_t n) -- { -- size_t i; -- -- for (i = 0; i < n && src[i] != '\0'; i++) { -- -- ---- -- char *my_strncpy(char *dest, const char *src, size_t n) -- { -- size_t i; -- -- for (i = 0; i < n && src[i] != '\0'; i++) { -- -- ------------------------------------------------------------- -- dest[i] = src[i]; -- } -- -- // If src is shorter than n, pad with '\0' -- -- ---- -- dest[i] = src[i]; -- } -- -- // If src is shorter than n, pad with '\0' -- -- ------------------------------------------------------------- -- for (; i < n; i++) { -- dest[i] = '\0'; -- } -- -- return dest; -- } -- ``` -- ---- -- for (; i < n; i++) { -- dest[i] = '\0'; -- } -- -- return dest; -- } -- -- ------------------------------------------------------------- -- ``` -- -- ### Explanation: -- -- * **Parameters:** -- -- * `dest`: destination buffer to c -- ---- -- </code></pre> -- <h3>Explanation:</h3> -- <ul> -- <li><strong>Parameters:</strong> -- ------------------------------------------------------------- -- * `dest`: destination buffer to copy into. -- * `src`: source string. -- * `n`: maximum number of bytes to copy. -- -- ---- -- <ul> -- <li><code>dest</code>: destination buffer to copy into.</li> -- <li><code>src</code>: source string.</li> -- <li><code>n</code>: maximum number of bytes to copy. -- ------------------------------------------------------------- -- -- -- * **Logic:** -- -- 1. Copy characters from `src` into `dest` until you hit eit -- ---- -- </li> -- </ul> -- </li> -- <li><strong>Logic:</strong> -- ------------------------------------------------------------- -- 1. Copy characters from `src` into `dest` until you hit either: -- -- * The null terminator (`'\0'`) in `src`, or -- * The limit `n`. -- -- ---- -- <ol> -- <li>Copy characters from <code>src</code> into <code>dest</code> until you hit either: -- <ul> -- <li>The null terminator (<code>'\0'</code>) in <code>src</code>, or</li> -- <li>The limit <code>n</code>. -- ------------------------------------------------------------- -- -- 2. If you reached the end of `src` before hitting `n`, pad the remaining byte -- ---- -- -- ------------------------------------------------------------- -- 2. If you reached the end of `src` before hitting `n`, pad the remaining bytes of `dest` with `'\0'`. -- 3. Return `dest` so it behaves like standard C libra -- ---- -- </li> -- </ul> -- </li> -- <li>If you reached the end of <code>src</code> before hitting <code>n</code>, pad the remaining bytes of <code>dest</code> with <code>'\0'</code>. -- ------------------------------------------------------------- -- 3. Return `dest` so it behaves like standard C library functions. -- -- ### Notes: -- -- * `strncpy` does **not** guarantee null-terminati -- ---- -- </li> -- <li>Return <code>dest</code> so it behaves like standard C library functions.</li> -- </ol> -- </li> -- </ul> -- <h3>Notes:</h3> -- -- ------------------------------------------------------------- -- * `strncpy` does **not** guarantee null-termination if `src` has length ≥ `n`. -- To ensure a null-terminated string, you can do: -- ---- -- <ul> -- <li><code>strncpy</code> does <strong>not</strong> guarantee null-termination if <code>src</code> has length ≥ <code>n</code>. -- ------------------------------------------------------------- -- To ensure a null-terminated string, you can do: -- -- ```c -- dest[n - 1] = '\0'; -- ``` -- -- — but only if you *always* reserve -- ---- -- -- To ensure a null-terminated string, you can do: -- <pre id="pre_80ca4db3-f745-473c-91d7-a565d106bac8"><code id="80ca4db3-f745-473c-91d7-a565d106bac8" class="language-c">dest[n - 1] = '\0'; -- </code></pre> -- -- ------------------------------------------------------------- -- — but only if you *always* reserve space for that final null. -- -- --- -- -- Would you like me to also show a **safer** -- ---- -- — but only if you <em>always</em> reserve space for that final null.</li> -- </ul> -- <hr /> -- -- ------------------------------------------------------------- -- Would you like me to also show a **safer** version (like how `strlcpy` behaves) that guarantees null-termination? -- -- ---- -- <p>Would you like me to also show a <strong>safer</strong> version (like how <code>strlcpy</code> behaves) that guarantees null-termination? -- ----FINAL---------------------------------------------------- -- -- -- ---- -- </p> -- ******************************* -- **** Full Accumulated HTML **** -- ******************************* -- <p>Here’s a simple implementation of the standard C library function <code>strncpy</code>, written from scratch — demonstrating how it works internally:</p> -- <pre id="pre_dd232b5f-2279-4841-b602-5840072f7a84"><code id="dd232b5f-2279-4841-b602-5840072f7a84" class="language-c">#include <stddef.h> // for size_t -- -- char *my_strncpy(char *dest, const char *src, size_t n) -- { -- size_t i; -- -- for (i = 0; i < n && src[i] != '\0'; i++) { -- dest[i] = src[i]; -- } -- -- // If src is shorter than n, pad with '\0' -- for (; i < n; i++) { -- dest[i] = '\0'; -- } -- -- return dest; -- } -- </code></pre> -- <h3>Explanation:</h3> -- <ul> -- <li><strong>Parameters:</strong><ul> -- <li><code>dest</code>: destination buffer to copy into.</li> -- <li><code>src</code>: source string.</li> -- <li><code>n</code>: maximum number of bytes to copy.</li> -- </ul> -- </li> -- <li><strong>Logic:</strong><ol> -- <li>Copy characters from <code>src</code> into <code>dest</code> until you hit either: -- <ul> -- <li>The null terminator (<code>'\0'</code>) in <code>src</code>, or</li> -- <li>The limit <code>n</code>.</li> -- </ul> -- </li> -- <li>If you reached the end of <code>src</code> before hitting <code>n</code>, pad the remaining bytes of <code>dest</code> with <code>'\0'</code>.</li> -- <li>Return <code>dest</code> so it behaves like standard C library functions.</li> -- </ol> -- </li> -- </ul> -- <h3>Notes:</h3> -- <ul> -- <li><code>strncpy</code> does <strong>not</strong> guarantee null-termination if <code>src</code> has length ≥ <code>n</code>. -- To ensure a null-terminated string, you can do: -- <pre id="pre_80ca4db3-f745-473c-91d7-a565d106bac8"><code id="80ca4db3-f745-473c-91d7-a565d106bac8" class="language-c">dest[n - 1] = '\0'; -- </code></pre> -- — but only if you <em>always</em> reserve space for that final null.</li> -- </ul> -- <hr /> -- <p>Would you like me to also show a <strong>safer</strong> version (like how <code>strlcpy</code> behaves) that EXEC @hr = sp_OADestroy @options EXEC @hr = sp_OADestroy @sbFullMarkdown EXEC @hr = sp_OADestroy @sbHtmlFrag EXEC @hr = sp_OADestroy @sbHtml EXEC @hr = sp_OADestroy @sbStreamingMarkdown EXEC @hr = sp_OADestroy @sbMdChunk END GO |
||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.