<?xml version="1.0" encoding="UTF-8"?>
<NeoAppBuilderPlugIn>
  <Title>neoRtf</Title>
  <Author>SinLios</Author>
  <Website>https://visualneo.com</Website>
  <EMail>info@sinlios.com</EMail>
  <Copyright>2021</Copyright>
  <Description>Converts Text and HTML to and from RTF format.</Description>
  <AutoInstall>no</AutoInstall>
  <Code>function neoTextToRtf(textString,retVar){
    plain = textString.replace(/\n/g, "\\par\n");
    $App[retVar] = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\n\\viewkind4\\uc1\\pard\\f0\\fs17 " + plain + "\\par\n}";
}
function neoRtfToText(rtfString,retVar){
   rtf = rtfString.replace(/\\par[d]?/g, "");
   $App[retVar] = rtf.replace(/\{\*?\\[^{}]+}|[{}]|\\\n?[A-Za-z]+\n?(?:-?\d+)?[ ]?/g, "").trim();
}
function neoHtmlToRtf(htmlString,retVar){
  var tmpRichText, hasHyperlinks;
  var richText = htmlString;

  // Singleton tags
  richText = richText.replace(/&lt;(?:hr)(?:\s+[^&gt;]*)?\s*[\/]?&gt;/ig, "{\\pard \\brdrb \\brdrs \\brdrw10 \\brsp20 \\par}\n{\\pard\\par}\n");
  richText = richText.replace(/&lt;(?:br)(?:\s+[^&gt;]*)?\s*[\/]?&gt;/ig, "{\\pard\\par}\n");

  // Empty tags
  richText = richText.replace(/&lt;(?:p|div|section|article)(?:\s+[^&gt;]*)?\s*[\/]&gt;/ig, "{\\pard\\par}\n");
  richText = richText.replace(/&lt;(?:[^&gt;]+)\/&gt;/g, "");

  // Hyperlinks
  richText = richText.replace(
      /&lt;a(?:\s+[^&gt;]*)?(?:\s+href=(["'])(?:javascript:void\(0?\);?|#|return false;?|void\(0?\);?|)\1)(?:\s+[^&gt;]*)?&gt;/ig,
      "{{{\n");
  tmpRichText = richText;
  richText = richText.replace(
      /&lt;a(?:\s+[^&gt;]*)?(?:\s+href=(["'])(.+)\1)(?:\s+[^&gt;]*)?&gt;/ig,
      "{\\field{\\*\\fldinst{HYPERLINK\n \"$2\"\n}}{\\fldrslt{\\ul\\cf1\n");
  hasHyperlinks = richText !== tmpRichText;
  richText = richText.replace(/&lt;a(?:\s+[^&gt;]*)?&gt;/ig, "{{{\n");
  richText = richText.replace(/&lt;\/a(?:\s+[^&gt;]*)?&gt;/ig, "\n}}}");

  // Start tags
  richText = richText.replace(/&lt;(?:b|strong)(?:\s+[^&gt;]*)?&gt;/ig, "{\\b\n");
  richText = richText.replace(/&lt;(?:i|em)(?:\s+[^&gt;]*)?&gt;/ig, "{\\i\n");
  richText = richText.replace(/&lt;(?:u|ins)(?:\s+[^&gt;]*)?&gt;/ig, "{\\ul\n");
  richText = richText.replace(/&lt;(?:strike|del)(?:\s+[^&gt;]*)?&gt;/ig, "{\\strike\n");
  richText = richText.replace(/&lt;sup(?:\s+[^&gt;]*)?&gt;/ig, "{\\super\n");
  richText = richText.replace(/&lt;sub(?:\s+[^&gt;]*)?&gt;/ig, "{\\sub\n");
  richText = richText.replace(/&lt;(?:p|div|section|article)(?:\s+[^&gt;]*)?&gt;/ig, "{\\pard\n");

  // End tags
  richText = richText.replace(/&lt;\/(?:p|div|section|article)(?:\s+[^&gt;]*)?&gt;/ig, "\n\\par}\n");
  richText = richText.replace(/&lt;\/(?:b|strong|i|em|u|ins|strike|del|sup|sub)(?:\s+[^&gt;]*)?&gt;/ig, "\n}");

  // Strip any other remaining HTML tags [but leave their contents]
  richText = richText.replace(/&lt;(?:[^&gt;]+)&gt;/g, "");

  // Prefix and suffix the rich text with the necessary syntax
  richText =
      "{\\rtf1\\ansi\n" + (hasHyperlinks ? "{\\colortbl\n;\n\\red0\\green0\\blue255;\n}\n" : "") + richText +  "\n}";

  $App[retVar] = richText;
}</Code>
  <Files/>
  <ActionList>
    <Action Name="neoTextToRtf">
      <Hint>Convert plain text to RTF format</Hint>
      <Params>textString|STRING|Text string;retVar|VARNAME|Variable to store result</Params>
      <Template>neoTextToRtf(textString,retVar);</Template>
    </Action>
    <Action Name="neoRtfToText">
      <Hint>Convert RTF to plain text format</Hint>
      <Params>rtfString|STRING|RTF string;retVar|VARNAME|Variable to store result</Params>
      <Template>neoRtfToText(rtfString,retVar);</Template>
    </Action>
    <Action Name="neoHtmlToRtf">
      <Hint>Convert HTML to RTF format</Hint>
      <Params>htmlString|STRING|HTML string;retVar|STRING|Variable tu store result</Params>
      <Template>neoHtmlToRtf(htmlString,retVar);</Template>
    </Action>
  </ActionList>
</NeoAppBuilderPlugIn>
