![]() |
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
(PowerBuilder) 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.
integer li_rc integer li_Success oleobject loo_Options oleobject loo_SbFullMarkdown oleobject loo_SbHtmlFrag oleobject loo_SbHtml oleobject loo_SbStreamingMarkdown oleobject loo_SbMdChunk integer li_Safety integer li_ChunkSize string s li_Success = 0 loo_Options = create oleobject li_rc = loo_Options.ConnectToNewObject("Chilkat.JsonObject") if li_rc < 0 then destroy loo_Options MessageBox("Error","Connecting to COM object failed") return end if loo_Options.UpdateString("theme","raw") // Indicate streaming mode loo_Options.UpdateBool("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. loo_SbFullMarkdown = create oleobject li_rc = loo_SbFullMarkdown.ConnectToNewObject("Chilkat.StringBuilder") li_Success = loo_SbFullMarkdown.LoadFile("qa_data/markdown/test1.md","utf-8") if li_Success = 0 then Write-Debug loo_SbFullMarkdown.LastErrorText destroy loo_Options destroy loo_SbFullMarkdown return end if // This will contain the fragment of HTMl produced at each step in streaming mode. loo_SbHtmlFrag = create oleobject li_rc = loo_SbHtmlFrag.ConnectToNewObject("Chilkat.StringBuilder") // We'll accumulate the full HTML result here, by appending each HTLM fragment loo_SbHtml = create oleobject li_rc = loo_SbHtml.ConnectToNewObject("Chilkat.StringBuilder") // This will contain the chunk of markdown not yet converted to HTML. loo_SbStreamingMarkdown = create oleobject li_rc = loo_SbStreamingMarkdown.ConnectToNewObject("Chilkat.StringBuilder") // This contains the current 80 byte markdown chunk to be processed. // The last chunk can be less than 80 bytes. loo_SbMdChunk = create oleobject li_rc = loo_SbMdChunk.ConnectToNewObject("Chilkat.StringBuilder") // 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. li_Safety = 0 do while (loo_SbFullMarkdown.Length > 0) AND (li_Safety < 2000) li_ChunkSize = 80 if loo_SbFullMarkdown.Length < li_ChunkSize then li_ChunkSize = loo_SbFullMarkdown.Length end if // Remove the leading chunk from the sbFullMarkdown and append to sbStreamingMarkdown. s = loo_SbFullMarkdown.GetRange(0,li_ChunkSize,1) loo_SbStreamingMarkdown.Append(s) Write-Debug "-------------------------------------------------------------" loo_SbStreamingMarkdown.ToCRLF() Write-Debug loo_SbStreamingMarkdown.GetAsString() // 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. loo_SbHtmlFrag.Clear() loo_SbStreamingMarkdown.MarkdownToHtml(loo_Options,loo_SbHtmlFrag) // Accumulate the HTML fragments into a full HTML document. loo_SbHtml.AppendSb(loo_SbHtmlFrag) Write-Debug "----" loo_SbHtmlFrag.ToCRLF() Write-Debug loo_SbHtmlFrag.GetAsString() li_Safety = li_Safety + 1 loop // Flush any remaining closing HTML tags by passing a final line-ending. Write-Debug "----FINAL----------------------------------------------------" loo_SbStreamingMarkdown.Append("~n") Write-Debug loo_SbStreamingMarkdown.GetAsString() loo_SbHtmlFrag.Clear() loo_SbStreamingMarkdown.MarkdownToHtml(loo_Options,loo_SbHtmlFrag) loo_SbHtml.AppendSb(loo_SbHtmlFrag) Write-Debug "----" Write-Debug loo_SbHtmlFrag.GetAsString() Write-Debug "" Write-Debug "**** Full Accumulated HTML ****" loo_SbHtml.ToCRLF() Write-Debug loo_SbHtml.GetAsString() // 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 destroy loo_Options destroy loo_SbFullMarkdown destroy loo_SbHtmlFrag destroy loo_SbHtml destroy loo_SbStreamingMarkdown destroy loo_SbMdChunk |
||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.