added a new item content type: markdown

- emacs editor has an option to select markdown, now it has two planes: one for content editing and the other to show preview
- CodeMirror editor updated to 5.64.0
- to convert markdown to html we use showdown https://github.com/showdownjs/showdown (conversion is done on the client side)
This commit is contained in:
Tomasz Sowa 2021-11-26 21:49:21 +01:00
parent 119497bb01
commit 35cb54324f
8 changed files with 355 additions and 73 deletions

View File

@ -614,7 +614,7 @@ void Functions::ReadItemUrlSubject(Item & item, Item::Type item_type)
}
void Functions::ReadItemFilterHtml(Item & item)
void Functions::ReadItemFilterHtml(const std::wstring & html, std::wstring & html_filtered)
{
//html_filter.BreakWord(0);
html_filter.WrapLine(0);
@ -624,65 +624,111 @@ void Functions::ReadItemFilterHtml(Item & item)
html_filter.ClearOrphans();
// SetNoFilterTag doesn't have to be called (default empty tag)
html_filter.filter(cur->request->PostVar(L"itemcontent"), item.item_content.content_raw);
//html_filter.filter(cur->request->PostVar(L"itemcontent"), item.item_content.content_raw);
html_filter.filter(html, html_filtered);
}
void Functions::ReadItemContent(Item & item, const std::wstring & content_type)
void Functions::ReadItemContent(Item & item)
{
bool filter_html = config->editors_html_safe_mode;
bool is_root = cur->session->puser && cur->session->puser->is_super_user;
bool filter_html = (content_type == L"2") && config->editors_html_safe_mode;
if( filter_html && is_root && config->editors_html_safe_mode_skip_root )
if( is_root && config->editors_html_safe_mode_skip_root )
filter_html = false;
if( filter_html )
ReadItemFilterHtml(item);
else
cur->request->PostVar(L"itemcontent", item.item_content.content_raw);
std::wstring * content = cur->request->PostVarp(L"itemcontent");
std::wstring * content_parsed = cur->request->PostVarp(L"itemcontent_parsed");
if( item.item_content.content_raw_type != ItemContent::ct_markdown )
{
item.item_content.content_parsed.clear();
}
if( content )
{
if( item.item_content.content_raw_type == ItemContent::ct_html )
{
if( filter_html )
{
ReadItemFilterHtml(*content, item.item_content.content_raw);
}
else
{
item.item_content.content_raw = *content;
}
}
else
{
item.item_content.content_raw = *content;
}
}
if( content_parsed )
{
if( item.item_content.content_raw_type == ItemContent::ct_markdown )
{
if( filter_html )
{
ReadItemFilterHtml(*content_parsed, item.item_content.content_parsed);
}
else
{
item.item_content.content_parsed = *content_parsed;
}
item.item_content.content_parsed_type = ItemContent::ct_html;
}
}
}
void Functions::ReadItemContentWithType(Item & item)
void Functions::ReadItemContentType(Item & item)
{
item.item_content.content_raw_type = ItemContent::ct_formatted_text; // default is formatted text
cur->request->PostVar(L"contenttype", temp);
item.item_content.content_parsed_type = ItemContent::ct_formatted_text;
std::wstring * content_type_str = cur->request->PostVarp(L"contenttype");
ReadItemContent(item, temp);
if( content_type_str )
item.item_content.content_raw_type = ItemContent::get_content_type_from_str(*content_type_str);
// ct_text and ct_formatted_text can use everyone
if( temp == L"0" )
item.item_content.content_raw_type = ItemContent::ct_text;
else
if( temp == L"1" )
item.item_content.content_raw_type = ItemContent::ct_formatted_text;
// those below need special privileges
if( !cur->session->puser )
return;
long user_id = cur->session->puser->id;
if( temp == L"2" )
if( item.item_content.content_raw_type != ItemContent::ct_text &&
item.item_content.content_raw_type != ItemContent::ct_formatted_text )
{
if( system->CanUseHtml(user_id) )
item.item_content.content_raw_type = ItemContent::ct_html;
}
else
if( temp == L"3" )
{
if( system->CanUseBBCode(user_id) )
item.item_content.content_raw_type = ItemContent::ct_bbcode;
}
else
if( temp == L"4" )
{
if( system->CanUseOther(user_id) )
item.item_content.content_raw_type = ItemContent::ct_other;
// ct_text and ct_formatted_text can use everyone
bool has_privileges = false;
if( cur->session->puser )
{
long user_id = cur->session->puser->id;
switch(item.item_content.content_raw_type)
{
case ItemContent::ct_html:
has_privileges = system->CanUseHtml(user_id);
break;
case ItemContent::ct_bbcode:
has_privileges = system->CanUseBBCode(user_id); // may test 'allow html' in such a case?
break;
case ItemContent::ct_other:
has_privileges = system->CanUseOther(user_id);
break;
case ItemContent::ct_markdown:
has_privileges = system->CanUseHtml(user_id);
break;
default:
break;
}
}
if( !has_privileges )
{
item.item_content.content_raw_type = ItemContent::ct_formatted_text;
}
}
}
@ -704,7 +750,10 @@ void Functions::ReadItem(Item & item, Item::Type item_type)
ReadItemUrlSubject(item, item_type);
if( item_type == Item::file )
ReadItemContentWithType(item);
{
ReadItemContentType(item);
ReadItemContent(item);
}
}

View File

@ -180,9 +180,9 @@ public:
bool CheckAbuse(SLog * slog = nullptr);
// !! dac lepsze nazwy
void ReadItemFilterHtml(Item & item);
void ReadItemContent(Item & item, const std::wstring & content_type);
void ReadItemContentWithType(Item & item);
void ReadItemFilterHtml(const std::wstring & html, std::wstring & html_filtered);
void ReadItemContent(Item & item);
void ReadItemContentType(Item & item);
// if item.url is not empty and there is not a post variable "url"
// then the item.url will not be changed
@ -205,7 +205,6 @@ private:
Templates * templates;
SessionManager * session_manager;
std::wstring temp;
pt::HTMLParser html_filter;
std::wstring link_to_temp;

View File

@ -1,6 +1,7 @@
<div class="uk-margin">
[if false]
[if winix_function_is "upload"]
<label class="uk-form-label" for="winix_content_id">{upload_content}</label>
[else]
@ -8,10 +9,56 @@
[if mount_type_is "thread"]<label class="uk-form-label" for="winix_content_id">{form_emacs_content_thread}</label>[end]
[if mount_type_is "ticket"]<label class="uk-form-label" for="winix_content_id">{form_emacs_content_ticket}</label>[end]
[end]
[end]
<div class="uk-form-controls">
[# CodeMirror doesn't work correctly inside fieldset tag -- horizontal scroll is broken]
<textarea class="uk-textarea" [if request.is_item]autofocus [end]id="winix_content_id" rows="[if winix_function_is "upload"]7[else][if mount_type_is "cms"]30[else]10[end][end]" cols="60" name="itemcontent">[item.content.content_raw]</textarea>
<div class="uk-flex uk-child-width-1-2">
<textarea
style="height: 1000px"
class="uk-textareaa"
[if request.is_item]autofocus [end]
id="winix_content_id"
rows="[if winix_function_is "upload"]7[else][if mount_type_is "cms"]300[else]300[end][end]"
cols="60" name="itemcontent">[item.content.content_raw]</textarea>
<div class="uk-margin-left" id="winix_output_preview">
</div>
</div>
[if false]
<ul uk-tab>
<li><a href="#">{upload_content_edit}</a></li>
<li><a href="#">{upload_content_preview}</a></li>
</ul>
<ul class="uk-switcher">
<li>
<textarea
style="height: 1000px"
class="uk-textarea"
[if request.is_item]autofocus [end]
id="winix_content_id"
rows="[if winix_function_is "upload"]7[else][if mount_type_is "cms"]300[else]300[end][end]"
cols="60" name="itemcontent">[item.content.content_raw]</textarea>
</li>
<li>
<div class="uk-margin-left" id="winix_output_preview">
</div>
</li>
</ul>
[end]
<input type="hidden" id="winix_content_parsed_id" value="" name="itemcontent_parsed">
</div>
</div>
@ -22,9 +69,16 @@
<option[if item.content.type_is "text"] selected[end] value="0">{form_emacs_content_type_text}</option>
<option[if item.content.type_is "formatted text"] selected[end] value="1">{form_emacs_content_type_formatted_text}</option>
[if user_can_use_html]<option[if item.content.type_is "html"] selected[end] value="2">{form_emacs_content_type_html}</option>[end]
[if user_can_use_bbcode]<option[if item.content.type_is "bbcode"] selected[end] value="3">{form_emacs_content_type_bbcode}</option>[end]
[if user_can_use_html]<option[if item.content.type_is "markdown"] selected[end] value="5">{form_emacs_content_type_markdown}</option>[end]
[if user_can_use_other]<option[if item.content.type_is "other"] selected[end] value="4">{form_emacs_content_type_other}</option>[end]
</select>
[if false]
[if user_can_use_bbcode]<option[if item.content.type_is "bbcode"] selected[end] value="3">{form_emacs_content_type_bbcode}</option>[end]
[end]
</div>

View File

@ -69,28 +69,36 @@
[if or [winix_function_is "emacs"] [winix_function_is "env"] [winix_function_is "meta"]]
<script type="text/javascript" src="[doc_base_url_common]/codemirror/5.59.2/lib/codemirror.js"></script>
<link rel="stylesheet" href="[doc_base_url_common]/codemirror/5.59.2/lib/codemirror.css" type="text/css">
<script type="text/javascript" src="[doc_base_url_common]/codemirror/5.64.0/lib/codemirror.js"></script>
<link rel="stylesheet" href="[doc_base_url_common]/codemirror/5.64.0/lib/codemirror.css" type="text/css">
<script src="[doc_base_url_common]/codemirror/5.59.2/mode/css/css.js"></script>
<script src="[doc_base_url_common]/codemirror/5.59.2/mode/javascript/javascript.js"></script>
<script src="[doc_base_url_common]/codemirror/5.59.2/mode/xml/xml.js"></script>
<script src="[doc_base_url_common]/codemirror/5.59.2/mode/htmlmixed/htmlmixed.js"></script>
<script src="[doc_base_url_common]/codemirror/5.64.0/mode/css/css.js"></script>
<script src="[doc_base_url_common]/codemirror/5.64.0/mode/javascript/javascript.js"></script>
<script src="[doc_base_url_common]/codemirror/5.64.0/mode/xml/xml.js"></script>
<script src="[doc_base_url_common]/codemirror/5.64.0/mode/htmlmixed/htmlmixed.js"></script>
<script src="[doc_base_url_common]/codemirror/5.64.0/mode/markdown/markdown.js"></script>
<!--<link rel="stylesheet" href="[doc_base_url_common]/codemirror/5.59.2/theme/base16-light.css" type="text/css">-->
<!--<link rel="stylesheet" href="[doc_base_url_common]/codemirror/5.64.0/theme/base16-light.css" type="text/css">-->
<script src="[doc_base_url_common]/codemirror/5.59.2/addon/display/fullscreen.js"></script>
<link rel="stylesheet" href="[doc_base_url_common]/codemirror/5.59.2/addon/display/fullscreen.css" type="text/css">
<script src="[doc_base_url_common]/codemirror/5.64.0/addon/display/fullscreen.js"></script>
<link rel="stylesheet" href="[doc_base_url_common]/codemirror/5.64.0/addon/display/fullscreen.css" type="text/css">
[if false]
<script src="[doc_base_url_common]/codemirror/5.59.2/addon/search/search.js"></script>
<link rel="stylesheet" href="[doc_base_url_common]/codemirror/5.59.2/addon/search/matchesonscrollbar.css" type="text/css">
<script src="[doc_base_url_common]/codemirror/5.64.0/addon/search/search.js"></script>
<link rel="stylesheet" href="[doc_base_url_common]/codemirror/5.64.0/addon/search/matchesonscrollbar.css" type="text/css">
[end]
<script src="[doc_base_url_common]/codemirror/5.59.2/addon/fold/xml-fold.js"></script>
<script src="[doc_base_url_common]/codemirror/5.59.2/addon/edit/matchtags.js"></script>
<script src="[doc_base_url_common]/codemirror/5.64.0/addon/fold/xml-fold.js"></script>
<script src="[doc_base_url_common]/codemirror/5.64.0/addon/edit/matchtags.js"></script>
<script src="[doc_base_url_common]/codemirror/5.64.0/addon/selection/active-line.js"></script>
[# markdown to html converter]
[# https://github.com/showdownjs/showdown ]
<script src="[doc_base_url_common]/showdown/1.9.1/dist/showdown.min.js"></script>
<script src="[doc_base_url_common]/codemirror/5.59.2/addon/selection/active-line.js"></script>
<style>
@ -112,14 +120,27 @@
<script type="text/javascript">
var winix_code_mirror_editor;
var winix_code_mirror_editor = null;
var winix_showdown_converter = null;
var winix_converter_output = null;
var winix_content_parsed = null;
var winix_emacs_mode = 0;
function winix_initialize_editor()
\{
var text_area = document.getElementById("winix_content_id");
var winix_contenttype = document.getElementById("winix_contenttype_id");
var onchange_delay = null;
winix_content_parsed = document.getElementById("winix_content_parsed_id");
if( text_area )
\{
winix_showdown_converter = new showdown.Converter();
winix_converter_output = document.getElementById("winix_output_preview");
winix_code_mirror_editor = CodeMirror.fromTextArea(text_area, \{
mode: "htmlmixed",
mode: winix_get_codemirror_mode(winix_emacs_mode),
theme: "default",
indentUnit: 4,
smartIndent: true,
@ -135,8 +156,98 @@
matchTags: true,
styleActiveLine: true
\});
winix_codemirror_preview();
winix_code_mirror_editor.on("change", function(instance) \{
if( onchange_delay !== null )
clearTimeout(onchange_delay);
onchange_delay = setTimeout(winix_codemirror_preview, 300);
\});
\}
if( winix_contenttype )
\{
winix_emacs_mode = parseInt(winix_contenttype.value);
winix_set_codemirror_mode();
winix_contenttype.addEventListener("change", function(e) \{
winix_emacs_mode = parseInt(this.value);
winix_set_codemirror_mode();
\});
\}
\}
function winix_codemirror_preview()
\{
if( winix_code_mirror_editor !== null )
\{
if( winix_emacs_mode == 2 )
\{
let sourceMarkup = winix_code_mirror_editor.getValue();
winix_converter_output.innerHTML = sourceMarkup;
if( winix_content_parsed !== null )
winix_content_parsed.value = "";
\}
else
if( winix_emacs_mode == 5 )
\{
let sourceMarkup = winix_code_mirror_editor.getValue();
let htmlMarkup = winix_showdown_converter.makeHtml(sourceMarkup);
if( winix_converter_output !== null )
winix_converter_output.innerHTML = htmlMarkup;
if( winix_content_parsed !== null )
winix_content_parsed.value = htmlMarkup;
\}
else
\{
[# IMPROVEME add support for text and formated text ]
winix_converter_output.innerHTML = "";
if( winix_content_parsed !== null )
winix_content_parsed.value = "";
\}
\}
\}
function winix_get_codemirror_mode(mode)
\{
switch(mode)
\{
case 5:
return "markdown";
case 2: // html
return "htmlmixed";
case 0: // text
case 1: // formatted text
case 3: // bbcode
case 4: // other
default:
return "text/plain";
\}
\}
function winix_set_codemirror_mode()
\{
if( winix_code_mirror_editor !== null )
\{
let codeMirrorMode = winix_get_codemirror_mode(winix_emacs_mode);
console.log("changing mode to: " + codeMirrorMode);
winix_code_mirror_editor.setOption("mode", codeMirrorMode);
winix_codemirror_preview();
\}
\}
[if false]
this will probably be used to change the mode in the fly
@ -147,11 +258,8 @@ cm.save()
Copy the content of the editor into the textarea.
[end]
if( window.addEventListener )
window.addEventListener("load", winix_initialize_editor, false);
else
if( window.attachEvent )
window.attachEvent("onload", winix_initialize_editor);
window.addEventListener("load", winix_initialize_editor, false);
</script>
[end]

View File

@ -167,6 +167,7 @@ form_emacs_content_type_formatted_text = formatted text
form_emacs_content_type_html = html
form_emacs_content_type_bbcode = bbcode
form_emacs_content_type_other = other \(no formatting\)
form_emacs_content_type_markdown = markdown
last_header = Last logged users
@ -341,6 +342,8 @@ upload_button_download = Download
upload_button_edit = Edit
upload_error = Error:
upload_processing = Processing...
upload_content_edit = Content
upload_content_preview = Preview
who_header = Sessions

View File

@ -172,6 +172,7 @@ form_emacs_content_type_formatted_text = sformatowany text
form_emacs_content_type_html = html
form_emacs_content_type_bbcode = bbcode
form_emacs_content_type_other = inny \(brak formatowania\)
form_emacs_content_type_markdown = markdown
last_header = Ostatnio logowani użytkownicy
@ -361,6 +362,9 @@ upload_button_download = Ściągnij
upload_button_edit = Edytuj
upload_error = Błąd:
upload_processing = Przygotowywanie...
upload_content_edit = Zawartość
upload_content_preview = Podgląd
who_header = Lista sesji
who_tab_index = L.p.

View File

@ -481,9 +481,26 @@ void ItemContent::print_content(HtmlTextStream & out, const std::wstring & conte
}
void ItemContent::print_content(HtmlTextStream & str)
{
Config * config = get_config();
bool is_html_filter_on = (config && config->html_filter);
if( content_raw_type == ct_markdown )
{
print_content(str, content_parsed, content_parsed_type, is_html_filter_on);
}
else
{
print_content(str, content_raw, content_raw_type, is_html_filter_on);
}
}
void ItemContent::print_content(EzcEnv & env)
{
print_content(env.out, content_raw, content_raw_type, true); // IMPROVE ME get the 'true' from the config (config->html_filter)
print_content(env.out);
}
@ -549,6 +566,45 @@ void ItemContent::group(morm::Wrapper & wrapper)
}
ItemContent::ContentType ItemContent::get_content_type_from_str(const wchar_t * str)
{
struct Type
{
const wchar_t * str;
ContentType type;
};
static Type types[] = {
{L"0", ct_text},
{L"1", ct_formatted_text},
{L"2", ct_html},
{L"3", ct_bbcode},
{L"4", ct_other},
{L"5", ct_markdown},
};
size_t len = sizeof(types) / sizeof(Type);
for(size_t i=0 ; i<len ; ++i)
{
if( pt::is_equal(types[i].str, str) )
{
return types[i].type;
}
}
return ct_other;
}
ItemContent::ContentType ItemContent::get_content_type_from_str(const std::wstring & str)
{
return get_content_type_from_str(str.c_str());
}
bool ItemContent::content_type_is(const std::wstring & type)
{
if( content_raw_type == ItemContent::ct_text && type == L"text" )
@ -565,6 +621,9 @@ bool ItemContent::content_type_is(const std::wstring & type)
else
if( content_raw_type == ItemContent::ct_other && type == L"other" )
return true;
else
if( content_raw_type == ItemContent::ct_markdown && type == L"markdown" )
return true;
return false;
}

View File

@ -94,6 +94,7 @@ public:
ct_html,
ct_bbcode,
ct_other, // no auto-formatting is applied
ct_markdown,
};
@ -278,6 +279,8 @@ public:
static void print_content(HtmlTextStream & out, const pt::WTextStream & content, ItemContent::ContentType content_type, bool is_html_filter_on);
static void print_content(HtmlTextStream & out, const std::wstring & content, ItemContent::ContentType content_type, bool is_html_filter_on);
void print_content(HtmlTextStream & str);
bool CanContentBeHtmlFiltered();
bool has_read_access(const User * current_user) const;
@ -304,6 +307,9 @@ public:
bool is_link_to() const;
bool is_link_redirect() const;
static ContentType get_content_type_from_str(const wchar_t * str);
static ContentType get_content_type_from_str(const std::wstring & str);
protected:
bool do_migration_to_1();