<commit_before><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<!-- dashboard template for slidoc server --->
<!-- edit parameters: site_name, session_name, session_label, session_text, err_msg -->

{% include "base.html" %}

<script>
var PreviewWin = null;

function slidocDiscard() {
   if (!window.confirm('Discard changes?')) {
     return false;
   }
   if (winURL(PreviewWin)) {
      PreviewWin.close();
   }
   PreviewWin = null;
   window.location = '{{site_prefix}}/_discard';
   return false;
}

function winURL(win) {
    // Returns window HREF (without hash), or null string on error
    if (!win)
       return '';
    try {
        return (win.location.origin == "null" ? 'about:' : win.location.origin) + win.location.pathname +
win.location.search;
    } catch (err) {
        return '';
    }
}

function checkPreviewWin() {
   var url = winURL(PreviewWin);
   if (!url)
      PreviewWin = null;
   return url;
}

function openWin(url, name) {
   // Open named window, if not already open
   // For previously open window with different url, switch to blank url
   var win = window.open("", name);

   if (win) {
      var utemp = winURL(win);
      console.log("openWin", utemp, ' URL:', url, win);
      if (utemp != url && utemp != 'about:blank')
         win.location.href = 'about:blank';
   } else {
      win = window.open(url, name);
      win.focus();
   }
   return win;
}

document.onkeydown = function(evt) {
   if ((evt.keyCode == 10 || evt.keyCode == 13) && evt.ctrlKey) {
      evt.stopPropagation();
      evt.preventDefault();
      slidocUpdate('update');
      return false;
   }
   if ((evt.keyCode == 10 || evt.keyCode == 13) && evt.altKey) {
      evt.stopPropagation();
      evt.preventDefault();
      if (PreviewWin && PreviewWin.Slidoc && PreviewWin.Slidoc.imageLink)
         document.getElementById('sessiontext').value += '\n\n' + PreviewWin.Slidoc.imageLink;
      return false;
   }
}

function slidocUpdate(action) {
   var xhr = new XMLHttpRequest();
   var previewPath = "{{site_prefix}}/_preview/index.html"
   var previewURL = location.origin+previewPath+"?update=1";
   var previewName = "{{site_name}}_preview";
   var indexSession = ("{{session_name}}" == "index");

   // Window opening must be triggered by user input
   if (action == 'open' || (action == 'update' && !checkPreviewWin())) {
      var msg = 'This will open a separate "live" preview  window/tab, which can be updated automatically. If you do not want live preview in a separate window, cancel now and use Save to preview in  this window.\nYou can use Control-Enter keystoke to trigger live updates and Alt-Enter to append image links.\nIf a tab is opened, you may want to drag it out and place it side-by-side with the edit window.';
      if (!window.confirm(msg)) {
         return false;
      }
      PreviewWin = openWin(previewURL, previewName);
   }

   slidocStatus('Processing ...');
   if (checkPreviewWin() && !PreviewWin.Slidoc && PreviewWin.document) {
      PreviewWin.document.body.textContent = 'Processing ...';
   }
   xhr.onreadystatechange = function () {
      var DONE = 4; // readyState 4 means the request is done.
      if (xhr.readyState === DONE) {
         if (xhr.status === 200) {

            if (xhr.getResponseHeader('Content-Type') === 'application/json') {
               var retval = null;
               try {
                  retval = JSON.parse(xhr.responseText);

                  if (retval.result === 'error') {
                     var errMsg = ''+retval.error;
                     slidocStatus(errMsg);
                     if (checkPreviewWin()) {
                         PreviewWin.Slidoc = null;
                         PreviewWin.document.body.textContent = errMsg;
                     }
                     return;
                  }
               } catch(err) {
               }
               console.log('retval=', retval);
            }

           if (action == 'save') {
              if (checkPreviewWin()) {
                 PreviewWin.close();
               }
               PreviewWin = null;
               slidocStatus('');
               window.location = previewPath;
           } else {
              if (checkPreviewWin()) {
                 if (checkPreviewWin() == 'about:blank') {
                    PreviewWin.location = previewURL;
                 } else if (indexSession || !PreviewWin.Slidoc) {
                    // Only reload for "simple pages" like ToC. Session pages will be automatically reloaded via websocket 
                    PreviewWin.location.reload(true);
                 }
                 slidocStatus('Updated preview window (use Ctl-Enter update and Alt-Enter to append image links)');
              }
           }

        } else {
          var errMsg = xhr.responseText+' (status='+xhr.status+')';
          if (errMsg.indexOf('MODIFY_SESSION') >=0) {
              errMsg = 'Retry with Modify ... option checked. ' + errMsg;
          }
          slidocStatus(errMsg);
          if (checkPreviewWin()) {
             PreviewWin.Slidoc = null;
             PreviewWin.document.body.textContent = errMsg;
          }
        }

      }
   };

   var data = new FormData(document.getElementById("slidoc-edit-form"));
   if (action != 'save')
      data.append('update', 1);
   xhr.open('POST', "{{site_prefix}}/_edit", true);
   xhr.send(data);
   return false;
}
function slidocStatus(text, show) {
   try { document.getElementById("slidoc-err-msg").textContent = text; } catch(err) {}
   if (show)
      alert(msg);
}
document.onreadystatechange = function(event) {
    if (document.readyState != "interactive" || !document.body)
        return;
    document.querySelectorAll('textarea').forEach(function(tx){
        tx.style.height = tx.scrollHeight + 12 + 'px';
    });
}
</script>

<style>
.slidoc-edit-preview {
    float: right;
}
</style>

</head>
<body>

<tt><div id="slidoc-err-msg">{{err_msg}}</div></tt>
<p></p>
<form action="{{site_prefix}}/_edit" method="post" id="slidoc-edit-form" enctype="multipart/form-data" >
  {% module xsrf_form_html() %}
  <div>
    <button type="button" onclick="slidocUpdate('save')">Save</button>
    <button type="button" onclick="slidocDiscard()">Discard</button>
    <button type="button" onclick="slidocUpdate('update')" class="slidoc-edit-preview">Update preview</button>
    <button type="button" onclick="slidocUpdate('open')" class="slidoc-edit-preview">Open preview</button>
  </div>
  <fieldset>
    <label for="sessionname">Module:</label> <b>{{ session_label }}</b>
    <input type="hidden" id="sessionname" name="sessionname" value="{{session_name}}"></input>
    &nbsp;&nbsp;
    <label for="sessionmodify">Modify question numbers/types?</label>
    <input type="checkbox" id="sessionmodify" name="sessionmodify" value="modify">
    <p></p>
    <textarea id="sessiontext" name="sessiontext" style="width: 90%;">{{session_text}}</textarea>
  </fieldset>
</form>

</body>
</html><commit_msg>Fixed preview updating and direct accept<commit_after><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<!-- dashboard template for slidoc server --->
<!-- edit parameters: site_name, session_name, session_label, session_text, err_msg -->

{% include "base.html" %}

<script>
// Global values
var PreviewWin = null;
var SessionText = null;

function slidocDiscard() {
   if (!window.confirm('Discard changes?')) {
     return false;
   }
   if (winURL(PreviewWin)) {
      PreviewWin.close();
   }
   PreviewWin = null;
   window.location = '{{site_prefix}}/_discard';
   return false;
}

function slidocAccept() {
   if (SessionText != document.getElementById('sessiontext').value) {
      if (!window.confirm('Content modified since last preview update. Discard those modifications?'))
         return false;
   }
   if (!window.confirm('Accept last preview?')) {
     return false;
   }
   if (winURL(PreviewWin)) {
      PreviewWin.close();
   }
   PreviewWin = null;
   window.location = '{{site_prefix}}/_accept';
   return false;
}

function winURL(win) {
    // Returns window HREF (without hash), or null string on error
    if (!win)
       return '';
    try {
        return (win.location.origin == "null" ? 'about:' : win.location.origin) + win.location.pathname +
win.location.search;
    } catch (err) {
        return '';
    }
}

function checkPreviewWin() {
   var url = winURL(PreviewWin);
   if (!url)
      PreviewWin = null;
   return url;
}

function openWin(url, name) {
   // Open named window, if not already open
   // For previously open window with different url, switch to blank url
   var win = window.open("", name);

   if (win) {
      var utemp = winURL(win);
      console.log("openWin", utemp, ' URL:', url, win);
      if (utemp != url && utemp != 'about:blank')
         win.location.href = 'about:blank';
   } else {
      win = window.open(url, name);
      win.focus();
   }
   return win;
}

document.onkeydown = function(evt) {
   if ((evt.keyCode == 10 || evt.keyCode == 13) && evt.ctrlKey) {
      evt.stopPropagation();
      evt.preventDefault();
      slidocUpdate('update');
      return false;
   }
   if ((evt.keyCode == 10 || evt.keyCode == 13) && evt.altKey) {
      evt.stopPropagation();
      evt.preventDefault();
      if (PreviewWin && PreviewWin.Slidoc && PreviewWin.Slidoc.imageLink)
         document.getElementById('sessiontext').value += '\n\n' + PreviewWin.Slidoc.imageLink;
      return false;
   }
}

function slidocUpdate(action) {
   var xhr = new XMLHttpRequest();
   var previewPath = "{{site_prefix}}/_preview/index.html"
   var previewURL = location.origin+previewPath+"?update=1";
   var previewName = "{{site_name}}_preview";
   var indexSession = ("{{session_name}}" == "index");

   // Window opening must be triggered by user input
   if (action == 'open' || (action == 'update' && !checkPreviewWin())) {
      var msg = 'This will open a separate "live" preview  window/tab, which can be updated automatically. If you do not want live preview in a separate window, cancel now and use Save to preview in  this window.\nYou can use Control-Enter keystoke to trigger live updates and Alt-Enter to append image links.\nIf a tab is opened, you may want to drag it out and place it side-by-side with the edit window.';
      if (!window.confirm(msg)) {
         return false;
      }
      PreviewWin = openWin(previewURL, previewName);
   }

   slidocStatus('Processing ...');
   if (checkPreviewWin() && !PreviewWin.Slidoc && PreviewWin.document) {
      PreviewWin.document.body.textContent = 'Processing ...';
   }
   xhr.onreadystatechange = function () {
      var DONE = 4; // readyState 4 means the request is done.
      if (xhr.readyState === DONE) {
         if (xhr.status === 200) {

            if (xhr.getResponseHeader('Content-Type') === 'application/json') {
               var retval = null;
               try {
                  retval = JSON.parse(xhr.responseText);

                  if (retval.result === 'error') {
                     var errMsg = ''+retval.error;
                     slidocStatus(errMsg);
                     if (checkPreviewWin()) {
                         PreviewWin.Slidoc = null;
                         PreviewWin.document.body.textContent = errMsg;
                     }
                     return;
                  }
               } catch(err) {
               }
               console.log('retval=', retval);
            }

           if (action == 'save') {
              if (checkPreviewWin()) {
                 PreviewWin.close();
               }
               PreviewWin = null;
               slidocStatus('');
               window.location = previewPath;
           } else {
              // open/update action
              if (checkPreviewWin()) {
                 if (checkPreviewWin() == 'about:blank') {
                    PreviewWin.location = previewURL;
                 } else if (!PreviewWin.Slidoc || !PreviewWin.GService) {
                    // Only reload for "simple pages" like ToC. Paced session pages will be automatically reloaded via websocket 
                    PreviewWin.location.reload(true);
                 }
                 slidocStatus('Updated preview window (use Ctl-Enter update and Alt-Enter to append image links)');
              }
           }

        } else {
          var errMsg = xhr.responseText+' (status='+xhr.status+')';
          if (errMsg.indexOf('MODIFY_SESSION') >=0) {
              errMsg = 'Retry with Modify ... option checked. ' + errMsg;
          }
          slidocStatus(errMsg);
          if (checkPreviewWin()) {
             PreviewWin.Slidoc = null;
             PreviewWin.document.body.textContent = errMsg;
          }
        }

      }
   };

   var data = new FormData(document.getElementById("slidoc-edit-form"));
   if (action != 'save') {
      data.append('update', 1);
      SessionText = document.getElementById('sessiontext').value;
   }
   xhr.open('POST', "{{site_prefix}}/_edit", true);
   xhr.send(data);
   return false;
}
function slidocStatus(text, show) {
   try { document.getElementById("slidoc-err-msg").textContent = text; } catch(err) {}
   if (show)
      alert(msg);
}
document.onreadystatechange = function(event) {
    if (document.readyState != "interactive" || !document.body)
        return;
    document.querySelectorAll('textarea').forEach(function(tx){
        tx.style.height = tx.scrollHeight + 12 + 'px';
    });
}
</script>

<style>
.slidoc-edit-preview {
    float: right;
}
</style>

</head>
<body>

<tt><div id="slidoc-err-msg">{{err_msg}}</div></tt>
<p></p>
<form action="{{site_prefix}}/_edit" method="post" id="slidoc-edit-form" enctype="multipart/form-data" >
  {% module xsrf_form_html() %}
  <div>
    <button type="button" onclick="slidocUpdate('save')">Save</button>
    <button type="button" onclick="slidocDiscard()">Discard</button>
    <button type="button" onclick="slidocAccept()" class="slidoc-edit-preview">Accept preview</button>
    <button type="button" onclick="slidocUpdate('update')" class="slidoc-edit-preview">Update preview</button>
    <button type="button" onclick="slidocUpdate('open')" class="slidoc-edit-preview">Open preview</button>
  </div>
  <fieldset>
    <label for="sessionname">Module:</label> <b>{{ session_label }}</b>
    <input type="hidden" id="sessionname" name="sessionname" value="{{session_name}}"></input>
    &nbsp;&nbsp;
    <label for="sessionmodify">Modify question numbers/types?</label>
    <input type="checkbox" id="sessionmodify" name="sessionmodify" value="modify">
    <p></p>
    <textarea id="sessiontext" name="sessiontext" style="width: 90%;">{{session_text}}</textarea>
  </fieldset>
</form>

</body>
</html>
{'fraction_non_alphanumeric': 0.14764781752766276, 'fraction_numerical': 0.0027153621614282807, 'mean_word_length': 2.489341544291805, 'pattern_counts': {'":': 0, '<': 97, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4416050', 'n_tokens_mistral': 4459, 'n_tokens_neox': 4359, 'n_words': 1039}