ide: Markdown export improvements

This commit is contained in:
Mirek Fidler 2026-01-27 15:05:53 +01:00
parent 00684eeddb
commit 1d6d0b2bf7
5 changed files with 71 additions and 14 deletions

View file

@ -259,8 +259,8 @@ void TopicEditor::FileBar(Bar& bar)
bar.Add("Export group to PDF..", THISBACK(ExportGroupPdf));
bar.Add("Export to HTML..", THISBACK(ExportHTML));
bar.Add("Export group to HTML..", THISBACK(ExportGroupHTML));
bar.Add("Export as GitHub Markdown", [=] {
ExportMarkdown(editor.GetQTF());
bar.Add("Export as GitHub Markdown..", [=] {
ExportMarkdown(editor.GetQTF(), GetFileTitle(topicpath));
});
}

View file

@ -116,6 +116,6 @@ void QTFEdit(String& text);
void IdeHelpButton(Button& help, const String& link);
void ExportMarkdown(const char *qtf);
void ExportMarkdown(const char *qtf, const char *name);
#endif

View file

@ -1,6 +1,11 @@
LAYOUT(ExportMDLayout, 400, 488)
ITEM(Upp::Label, dv___0, SetLabel(t_("\001[g Markdown text copied! Since Markdown doesn't natively embed images, please use the table to manually copy images for their respective placeholders (IMAGE:N).")).SetVAlign(Upp::ALIGN_TOP).LeftPosZ(160, 236).TopPosZ(4, 88))
ITEM(Upp::Label, dv___0, SetLabel(t_("\001[g Markdown text copied! Since Markdown doesn't natively embed images, please use the table to manually copy images for their respective placeholders (IMAGE:N).")).SetVAlign(Upp::ALIGN_TOP).LeftPosZ(160, 236).TopPosZ(4, 80))
ITEM(Upp::ArrayCtrl, list, LeftPosZ(4, 150).TopPosZ(4, 480))
ITEM(Upp::Button, exit, SetLabel(t_("Close")).LeftPosZ(332, 64).TopPosZ(460, 24))
ITEM(Upp::Button, doexport, SetLabel(t_("Export")).LeftPosZ(332, 64).TopPosZ(148, 24))
ITEM(Upp::Label, dv___4, SetLabel(t_("\001[g Alternatively, you can export Markdown to a folder with image files:")).SetVAlign(Upp::ALIGN_TOP).LeftPosZ(160, 236).TopPosZ(88, 32))
ITEM(Upp::EditString, dir, LeftPosZ(160, 212).TopPosZ(124, 19))
ITEM(Upp::Button, sel, LeftPosZ(376, 20).TopPosZ(124, 20))
ITEM(Upp::ImageCtrl, preview, LeftPosZ(156, 240).TopPosZ(212, 244))
END_LAYOUT

View file

@ -50,8 +50,8 @@ void IdeQtfDes::EditMenu(Bar& menu)
{
EditTools(menu);
menu.Separator();
menu.Add("Export as GitHub Markdown", [=] {
ExportMarkdown(GetQTF());
menu.Add("Export as GitHub Markdown..", [=] {
ExportMarkdown(GetQTF(), GetFileTitle(filename));
});
}

View file

@ -4,6 +4,9 @@
#include <CtrlCore/lay.h>
struct ExportMD : WithExportMDLayout<TopWindow> {
bool exporting;
String qtf;
String name;
String md;
Vector<RichObject> img;
@ -13,7 +16,7 @@ struct ExportMD : WithExportMDLayout<TopWindow> {
static bool IsPreformatted(const RichPara& p);
void Do(const char *qtf);
void Do(const char *qtf, const char *name);
ExportMD();
};
@ -21,6 +24,39 @@ struct ExportMD : WithExportMDLayout<TopWindow> {
ExportMD::ExportMD()
{
CtrlLayoutExit(*this, "Export as GitHub Markdown");
sel.SetImage(Upp::CtrlImg::Dir());
sel << [=] {
dir <<= Nvl(SelectDirectory(), ~~dir);
};
list.NoWantFocus();
doexport << [=] {
exporting = true;
Export(ParseQTF(qtf));
String d = ~dir;
if(!DirectoryExists(d)) {
if(!PromptYesNo("Create directory [* \1 " + d))
return;
if(!RealizeDirectory(d))
Exclamation("Cannot create [* \1 " + d);
}
Progress pi("Exporting", 2 * img.GetCount());
SaveFile(d + "/" + name + ".md", md);
for(int i = 0; i < img.GetCount(); i++) {
for(int half = 0; half < 2; half++) {
if(pi.StepCanceled())
return;
RichObjectPaintInfo pi;
pi.ink = SBlack();
Image m = img[i].ToImage(img[i].GetPixelSize(), pi);
PNGEncoder().SaveFile(d + "/" + AsString(i) + (half ? "_half.png" : ".png"),
half ? Downscale2x(m) : m);
}
}
exporting = false;
Export(ParseQTF(qtf));
};
}
void ExportMD::Export(const RichPara& p)
@ -33,7 +69,11 @@ void ExportMD::Export(const RichPara& p)
const RichPara::Part& part = p.part[i];
int q;
if(part.object) {
md << "IMAGE:" << img.GetCount();
int n = img.GetCount();
if(exporting)
md << "![IMAGE " << n << "](" << n << ".png)";
else
md << "IMAGE:" << n;
img << part.object;
}
else {
@ -127,6 +167,8 @@ bool ExportMD::IsPreformatted(const RichPara& p)
void ExportMD::Export(const RichText& txt)
{
img.Clear();
md.Clear();
int i = 0;
while(i < txt.GetPartCount())
if(txt.IsPara(i)) {
@ -155,34 +197,44 @@ void ExportMD::Export(const RichText& txt)
i++;
}
void ExportMD::Do(const char *qtf)
void ExportMD::Do(const char *qtf_, const char *name_)
{
name = name_;
dir <<= GetHomeDirectory() + "/" + name;
qtf = qtf_;
Export(ParseQTF(qtf));
WriteClipboardText(md);
if(img.GetCount()) {
list.SetLineCy(DPI(28));
list.AddColumn("Image").Ctrls([&](int ii, One<Ctrl>& ctrl) {
int half = ii & 1;
ii /= 2;
Button& b = ctrl.Create<Button>();
b.NoWantFocus();
b.SetImage(CtrlImg::copy());
b.SetLabel("Copy IMAGE:" + AsString(ii));
b.SetLabel("Copy IMAGE:" + AsString(ii) + (half ? " 50%" : ""));
b << [=] {
if(ii >= 0 && ii < img.GetCount()) {
RichObjectPaintInfo pi;
pi.ink = SBlack();
WriteClipboardImage(img[ii].ToImage(img[ii].GetPixelSize(), pi));
Image m = img[ii].ToImage(img[ii].GetPixelSize(), pi);
preview.SetImage(CoRescaleFilter(m, GetFitSize(m.GetSize(), preview.GetSize()), FILTER_BILINEAR));
WriteClipboardImage(half ? Downscale2x(m) : m);
}
};
});
for(int i = 0; i < img.GetCount(); i++)
for(int i = 0; i < img.GetCount(); i++) {
list.Add();
list.Add();
}
Run();
}
else
PromptOK("Markdown copied to clipboard.");
}
void ExportMarkdown(const char *qtf)
void ExportMarkdown(const char *qtf, const char *name)
{
ExportMD md;
md.Do(qtf);
md.Do(qtf, name);
}