%* Richard A. DeVenezia;
%* http://www.devenezia.com;
%* March 2002
%* ;

/*-----
 * group: Data presentation
 * purpose: ODS HTML Javascripted titles and footnotes of unlimited length.<BR>Sample <A HREF="/cgi-bin/push/jsTitleAndFootnote-sample.sas">SAS program</A>, <A HREF="jsTitleAndFootnote-sample.html">HTML output</A>, <A HREF="jsTitleAndFootnote-sample.js.html">Javascript generated</A>
 * notes: Developed to work around <A HREF="https://web.archive.org/web/20080907104355/http://support.sas.com/techsup/unotes/SN/005/005118.html">SN-005118</A>, long TITLE and FOOTNOTE statements receive an unwanted &lt;BR&gt; at or near position 108 when using ODS HTML<BR><A HREF="jsTitleAndFootnote-sample.html">Usage</A>
 */

%* global macro variables used amongst several macros;
%global jsTitleAndFootnoteFileref;
%global jsTitleAndFootnoteUseCount;

%macro jsWriter(type,n,string,fileref,justify=left);
  %* Richard A. DeVenezia;
  %* http://www.devenezia.com;

  %* type    - tag, title or footnote;
  %* n       - N of footnote or title;
  %* string  - text to appear in title or footnote;
  %* fileref - fileref to write to;
  %* justify - reserved for future use;

  %local fid rc i L L2 indent fileref this useCount position useString;

  %let this = jsWriter;

  %if (&fileref eq %str()) %then %do;
    %put WARNING: &this;
    %put WARNING: No where to write since jsTitleFootnoteReset() was unsuccessful.;
    %goto macroExit;
  %end;

  %let fid = %sysfunc (fopen (&fileref, A));
  %if &fid %then %do;
    %if &type = tag %then %do;
      %let rc = %sysfunc (fput(&fid, // Generated by SAS macro &this %sysfunc(datetime(),datetime16.)));
      %let rc = %sysfunc (fwrite(&fid));
      %let rc = %sysfunc (fput(&fid, // Richard A. DeVenezia ));
      %let rc = %sysfunc (fwrite(&fid));
      %let rc = %sysfunc (fput(&fid, // http://www.devenezia.com ));
      %let rc = %sysfunc (fwrite(&fid));
      %let rc = %sysfunc (fput(&fid, %str() ));
      %let rc = %sysfunc (fwrite(&fid));
      %put NOTE: jsWriter wrote a datestamp comment.;
    %end;
    %else
    %if &type = title or &type = footnote %then %do;

      %if (&n < 1 or &n > 10) %then %do;
        %put WARNING: &this;
        %put WARNING: &type&n rejected. Number [&n] should be from 1 to 10.;
        %goto macroExit;
      %end;

      %if &type = title %then %do;
        %let position = %eval(&N*4+1);
        %let useCount = %substr (&jsTitleAndFootnoteUseCount,&position,3);
        %let useCount = %eval (&useCount+1);
      %end;
      %else %do;
        %let position = %eval(&N*4+41);
        %let useCount = %substr (&jsTitleAndFootnoteUseCount,&position,3);
        %let useCount = %eval (&useCount+1);
      %end;

      %let jsTitleAndFootnoteUseCount
      = %substr(&jsTitleAndFootnoteUseCount,1,%eval(&position-1))%sysfunc(putn(&useCount,z3.))%substr(&jsTitleAndFootnoteUseCount,%eval(&position+3));

      %if &useCount > 1 %then
        %let useString = use&useCount;

      %let rc = %sysfunc (fput(&fid, function &type.&N.&useString. %str(%(%)) { ));
      %let rc = %sysfunc (fwrite(&fid));
      %let rc = %sysfunc (fput(&fid, %str(  )document.write %str(%() ));
      %let rc = %sysfunc (fwrite(&fid));

      %let i = 1;
      %let L = %length (%superq(string));

      %let indent = %str (    );

      %do %while (&i <= &L);

        %let L2 = %eval (&L - &i + 1);
        %if &L2 > 100 %then %let L2 = 100;
        %let piece = %qsubstr(%superq(string),&i,&L2);

        %let rc = %sysfunc (fput(&fid, &indent"&piece" ));
        %let rc = %sysfunc (fwrite(&fid));

        %let i = %eval (&i + 100);
        %let indent = %str (  + );
      %end;

      %let rc = %sysfunc (fput(&fid, %str(  %);) ));
      %let rc = %sysfunc (fwrite(&fid));
      %let rc = %sysfunc (fput(&fid, } ));
      %let rc = %sysfunc (fwrite(&fid));
      %let rc = %sysfunc (fput(&fid, %str() ));
      %let rc = %sysfunc (fwrite(&fid));

      &type&n JUSTIFY=&JUSTIFY "<SCRIPT LANGUAGE=""JAVASCRIPT"">&type&n&useString()%str(;)</SCRIPT>";

      %put NOTE: jsWriter wrote &type&n..;
    %end;
    %let rc = %sysfunc (fclose (&fid));
  %end;
  %else
    %put ERROR: jsWriter could not open %sysfunc(pathname(&fileref)).;

%macroExit:
%mend jsWriter;

%macro jsTitleFootnoteReset(fileref);
  %* Richard A. DeVenezia;
  %* http://www.devenezia.com;

  %global jsTitleAndFootnoteFileref;
  %let jsTitleAndFootnoteFileref=;

  %global jsTitleAndFootnoteUseCount;
  %let jsTitleAndFootnoteUseCount = --- %sysfunc(repeat(%str(000 ),19))----;

  %local length rc this;

  %let length = 0;
  %let valid = 0;
  %let this = jsTitleFootnoteReset;

  %let length = %length (&fileref);
  %if &length = 0 %then %do;
    %put ERROR: &this: Null argument is invalid. Pass a defined FILEREF.;
    %goto errorExit;
  %end;

  %if &length > 8 %then %do;
    %put ERROR: &this: [&fileref] is too long.  Pass a defined FILEREF.;
    %goto errorExit;
  %end;

  %let rc = %sysfunc(fileref(&fileref));

  %if &rc > 0 %then %do;
    %put ERROR: &this: FILEREF [&fileref] is either undefined or invalid.;
    %goto errorExit;
  %end;

  %if &rc = 0 %then %do;
    %put WARNING: &this: The file [%sysfunc(pathname(&fileref))] will be overwritten.;

    %let rc = %sysfunc (fdelete(&fileref));
    %if &rc ne 0 %then %do;
      %put ERROR: &this: The file [%sysfunc(pathname(&fileref))] could not be deleted.;
      %goto macroExit;
    %end;
  %end;
  %else %do;
    %put NOTE: &this: The file [%sysfunc(pathname(&fileref))] will be created.;
  %end;

  %jsWriter (tag,0,0,fileref=&fileref);

  %if %sysfunc(fileref(&fileref)) = 0 %then %do;
    %let jsTitleAndFootnoteFileref = &fileref;

    title;
    footnote;
  %end;
  %else %do;
    %goto errorExit;
  %end;

  %goto macroExit;

%errorExit:
  %put ERROR: &this was unsuccessful.;

%macroExit:
%mend jsTitleFootnoteReset;

%macro jsTitle(n,string);
  %* Richard A. DeVenezia;
  %* http://www.devenezia.com;

  %jsWriter (title, &n, &string, &jsTitleAndFootnoteFileref);
%mend;

%macro jsFootnote(n,string);
  %* Richard A. DeVenezia;
  %* http://www.devenezia.com;

  %jsWriter (footnote, &n, &string, &jsTitleAndFootnoteFileref);
%mend;

%macro jsODSHeadText(filename);
  %* Richard A. DeVenezia;
  %* http://www.devenezia.com;

  <SCRIPT LANGUAGE=""JAVASCRIPT"" SRC=""&filename""></SCRIPT>
%mend;
