Seems like this should be an easy task...Use a “textarea” to display all of the code snippets in the recipes as code, indent any code snippets, and I’m done. HA!
It turned out that it would have been an amazingly complex task to tag all of the code and URL snippets and when I tried, I kept running into blank walls.
I decided to use what Jason Sauchuk, a Interactive Tools programmer discovered as an approach. As part of the process, he suggested using the PHP function “highlight_string” to convert the entire recipe into text with highlighted code segments.
Note: To change the default colors for the various code types in highlight_string to black I inserted the following in the viewer code.
<?php ini_set("highlight.comment", "#000"); ini_set("highlight.default", "#000"); ini_set("highlight.html", "#000"); ini_set("highlight.keyword", "#000;"); ini_set("highlight.string", "#000"); ?>
Here’s the system I developed:
1) To identify the line breaks that I ultimately wanted to show in the recipe, I did a search and replace on my recipe code for any of the three variants of a line break z-x, z-x with a /, and z-x with a space and a /, ( no dashes) and replaced them all with the characters “z followed by x” (no spaces). I could have used any unique character group that wasn’t likely to show up in the recipe.
2) Then in the viewer I used preg_replace to strip all remaining line breaks in the recipe.
3) And a preg_replace to convert all occurrences of the characters “z followed by x” (no spaces) back to z-x (no dash)
4) I used the wordwrap function to limit the length of the lines displayed in the recipe to 120 characters.
5) The code snippet indicators q followed by z followed by l, and l followed by z followed by q, (no spaces) were all manually inserted into the recipes around any code that I wanted to display as code.
6) I created a css style called "snippet" to style the code snippets and one to style any <code> tags
7) I used str_replace to place blockquote tags around the code snippets
In December of 2018 I wanted to automatically turn any complete URLs that were outside of any code blocks in the recipe into clickable links, while leaving those inside of any code blocks alone.
For that, Daniel Loewe from interactive Tools suggested using the htmlPurify function built in to CMSB. He was also extremely helpful in working out the code to separate the code blocks from the text.
Here’s the code we finally came up with:
*** Please note, since the cookbook code uses all instances of q-z-l and l-z-q (without the dashes), and all instances of z-x (without the dash) to as replacement keys, you MUST remove the dashes between those letters below for the code to work***
For the css style sheet:
code { font-family: Verdana, sans-serif; color: #000000; font-size: 1em; font-weight:normal; } .snippet { font-family: Verdana, sans-serif; color: #000000; font-size: 1em; font-weight:normal; margin: 0px 0px 0px 15px; padding: 8px 12px; border: 1px dashed #305555; background-color: #54ccf2; display: block; overflow-x: auto; }
For the body of the full cookbook viewer:
<?php ini_set("highlight.comment", "#000"); ini_set("highlight.default", "#000"); ini_set("highlight.html", "#000"); ini_set("highlight.keyword", "#000;"); ini_set("highlight.string", "#000"); ?> <table align="center" width="75%" border="0" cellpadding="0"> <?php foreach ($table_of_contentsRecords as $record): ?> <tr> <td align="left" valign="top"><!--remove all remaining line breaks from the code not converted to <br /> in wordprocessor--> <?PHP $record['recipe'] = preg_replace("[]", "", $record['recipe'] ); // insert <b-r> (remove the dash) between [ and ] for the code to work ?> <?PHP $record['recipe'] = preg_replace("[]", "", $record['recipe'] ); // insert <b-r/> (remove the dash) between [ and ] for the code to work ?> <?PHP $record['recipe'] = preg_replace("[]", "", $record['recipe'] ); // insert <b-r /> (remove the dash) between [ and ] for the code to work ?> <!--replace the valid <br in the code segment --> <?PHP $record['recipe'] = preg_replace("[z-x]i", "", $record['recipe'] ); // insert <b-r /> (remove the dash) between " and ", and remove the dash between z and x for the code to work ?> <?PHP $newUnixTime = strtotime($record['createdDate']); // seconds since 1970 to createdDate $updateUnixTime = strtotime($record['updatedDate']); // seconds since 1970 to updatedDate $pastUnixTime = strtotime(date('Y-m-d', strtotime("-30 day"))); $newdifference = $newUnixTime - $pastUnixTime ; $updatedifference = $updateUnixTime - $pastUnixTime ; ?> <?php if (!$record['recipe'] ): ?> <h2 align="center" class="Heading-Black"> <?php $topic = htmlspecialchars($record['topic']); ?> <?php echo strtoupper($topic); ?></h2> <?php else: ?> <hr color="#000000" align="center" width="600" /> <!-- Check For New Or Revised Date --> <?php if ($record['new'] == 1 && $newdifference >= 0): ?> <span class="Medium-Text-Bold-Red">NEW</span><span class="Medium-Text-Bold"> - </span> <?php endif ?> <?php if ($record['revised'] == 1 && $updatedifference >= 0): ?> <span class="Medium-Text-Bold-Red">REVISED</span><span class="Medium-Text-Bold"> - </span> <?php endif ?> <!-- End Date Check --> <div align="center" style="width:60%; text-align:left;"><span class="Medium-Text-Bold"> <?php $topic = htmlspecialchars($record['topic']); ?> <?php echo strtoupper($topic); ?> <?PHP if ($record['heirarchy'] == "Sub Topic"): ?> - <?php echo date("M jS, Y", strtotime($record['updatedDate'])) ?> <?php endif ?> </span> <?php endif ?> <span class="Medium-Text"> <?php $recipe = $record['recipe']; $recipe = wordwrap($recipe, 120) ; $recipe = highlight_string($recipe, true);
// Added per Daniel Loewe 12/27/18 to create links from URLs $config = array(); $config['AutoFormat.Linkify'] = true; // split on opening blockquote $chunks = explode('q-z-l', $recipe); // remove the dashes between the q, z and l for the code to work
// loop through each chunk foreach ($chunks as $key => $chunk) {
// split chunk on end blockquote $tmp = explode('l-z-q', $chunk); // remove the dashes between the q, z and l for the code to work // did we find an ending blockquote? if (count( $tmp ) > 1) {
// purify content after blockquote $tmp[1] = htmlPurify($tmp[1], $config); } else {
// purify only element $tmp[0] = htmlPurify($tmp[0], $config); } // replace ending blockquote $chunks[ $key ] = implode('l-z-q', $tmp); }
// replace opening blockquote $recipe = implode('q-z-l', $chunks); // remove the dashes between the q, z and l for the code to work
// reset block quotes $recipe = str_replace('q-z-l', '<blockquote class="snippet"> ', $recipe); // remove the dashes between the q, z and l for the code to work $recipe = str_replace('l-z-q', '</blockquote> ', $recipe); // remove the dashes between the q, z and l for the code to work ?> <?php echo $recipe; ?> </div></td> </tr> <?php endforeach; ?> </table>
And here's the code for the detail pages
<?php ini_set("highlight.comment", "#000"); ini_set("highlight.default", "#000"); ini_set("highlight.html", "#000"); ini_set("highlight.keyword", "#000;"); ini_set("highlight.string", "#000"); ?> <!--remove all remaining line breaks from the code not converted to <br /> in wordprocessor--> <?php $recipe = ($table_of_contentsRecord['recipe']); ?> <?PHP $recipe = preg_replace("[]", "", $recipe ); // insert <b-r> (remove the dash) between [ and ] for the code to work ?> <?PHP $recipe = preg_replace("[]", "", $recipe // insert <b-r/> (remove the dash) between [ and ] for the code to work ?> <?PHP $recipe = preg_replace("[]", "", $recipe ); // insert <b-r /> (remove the dash) between [ and ] for the code to work ?> <!--replace the valid <br in the code segment --> <?PHP $recipe = preg_replace("[z-x]i", "", $recipe ); // insert <b-r /> (remove the dash) between " and ", and remove the dash between z and x for the code to work ?> <?php $recipe = wordwrap($recipe, 120) ; ?> <?php $recipe = highlight_string($recipe, true); ?> <?php $config = array(); $config['AutoFormat.Linkify'] = true; // split on opening blockquote $chunks = explode('q-z-l', $recipe); // remove the dashes between the q, z and l for the code to work
// loop through each chunk foreach ($chunks as $key => $chunk) {
// split chunk on end blockquote $tmp = explode('l-z-q', $chunk); // remove the dashes between the q, z and l for the code to work // did we find an ending blockquote? if (count( $tmp ) > 1) {
// purify content after blockquote $tmp[1] = htmlPurify($tmp[1], $config); } else {
// purify only element $tmp[0] = htmlPurify($tmp[0], $config); } // replace ending blockquote $chunks[ $key ] = implode('l-z-q', $tmp); // remove the dashes between the q, z and l for the code to work }
// replace opening blockquote $recipe = implode('q-z-l', $chunks); // remove the dashes between the q, z and l for the code to work
// reset block quotes $recipe = str_replace('q-z-l', '<blockquote class="snippet"> ', $recipe); // remove the dashes between the q, z and l for the code to work $recipe = str_replace('l-z-q', '</blockquote> ', $recipe); // remove the dashes between the q, z and l for the code to work ?> <table align="center" width="100%" border="0"> <tr><td> <span class="Medium-Text-Bold"><b> <?php $topic = htmlspecialchars($table_of_contentsRecord['topic']); ?> <?php echo strtoupper($topic); ?> - <?php echo date("M jS, Y", strtotime($table_of_contentsRecord['updatedDate'])) ?></span> </td></tr> <tr> <td class="Medium-Text"><?php echo $recipe; ?></td> </tr> </table>
|