mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 14:16:07 -06:00
reference: Added markdown example (with image support). (#212)
* reference: Added markdown example (with image support). * Reference/MarkdownViewer: Unnecessary SSL package removed. * reference/MarkdownViewer: Unnecessary build flag removed, menu functions restructured. * reference/MarkdownViewer: A default markdown example file (with image) added.
This commit is contained in:
parent
678289f77f
commit
aa31f3e84a
4 changed files with 133 additions and 0 deletions
16
reference/MarkdownViewer/MarkdownViewer.upp
Normal file
16
reference/MarkdownViewer/MarkdownViewer.upp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
description "A simple markdown viewer with image displaying capability.\377";
|
||||
|
||||
uses
|
||||
CtrlLib,
|
||||
Painter,
|
||||
plugin/jpg,
|
||||
plugin/md;
|
||||
|
||||
file
|
||||
main.cpp,
|
||||
Resources readonly separator,
|
||||
example.md;
|
||||
|
||||
mainconfig
|
||||
"" = "GUI";
|
||||
|
||||
44
reference/MarkdownViewer/example.md
Normal file
44
reference/MarkdownViewer/example.md
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
|
||||
# Markdown Test Document
|
||||
|
||||
Markdown is a lightweight markup language with plain text formatting syntax. Its design allows it to be converted to many formats, but it's primarily used for formatting readme files, writing messages in online discussion forums, and creating rich text using a plain text editor.
|
||||
|
||||
## Links
|
||||
|
||||
Here are some examples of links:
|
||||
|
||||
- [U++](https://www.ultimatepp.org)
|
||||
- [Markdown Guide](https://www.markdownguide.org)
|
||||
|
||||
## Images
|
||||
|
||||
You can embed images:
|
||||
|
||||

|
||||
|
||||
## Lists
|
||||
|
||||
### Unordered List
|
||||
- Item 1
|
||||
- Item 2
|
||||
- Subitem 2.1
|
||||
- Subitem 2.2
|
||||
- Item 3
|
||||
|
||||
### Ordered List
|
||||
1. First item
|
||||
2. Second item
|
||||
3. Third item
|
||||
|
||||
## Code Blocks
|
||||
|
||||
You can include code blocks:
|
||||
|
||||
```C++
|
||||
int main()
|
||||
{
|
||||
printf("Hello, world!");
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
73
reference/MarkdownViewer/main.cpp
Normal file
73
reference/MarkdownViewer/main.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#include <CtrlLib/CtrlLib.h>
|
||||
|
||||
#include <plugin/md/Markdown.h>
|
||||
#include <plugin/bmp/bmp.h>
|
||||
#include <plugin/jpg/jpg.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
struct MarkdownViewer : TopWindow {
|
||||
RichTextView view;
|
||||
MenuBar menubar;
|
||||
Array<QtfRichObject> images;
|
||||
|
||||
MarkdownViewer()
|
||||
{
|
||||
Title(t_("A simple markdown viewer"));
|
||||
Sizeable().Zoomable().SetRect(0, 0, 800, 600);
|
||||
Add(view.SizePos());
|
||||
AddFrame(menubar);
|
||||
menubar.Set([this](Bar& menu)
|
||||
{
|
||||
menu.Sub(t_("File"), [this](Bar& menu)
|
||||
{
|
||||
menu.Add(t_("Open"), [this] { OpenFile(); })
|
||||
.Key(K_CTRL_O);
|
||||
menu.Add(t_("Copy to clipboard (as qtf)"),[this] { CopyAsQtf(); })
|
||||
.Key(K_CTRL_C)
|
||||
.Enable(!IsNull(~view));
|
||||
menu.Separator();
|
||||
menu.Add(t_("Exit"), THISFN(Close))
|
||||
.Key(K_CTRL_Q);
|
||||
});
|
||||
});
|
||||
ParseFile(GetDataFile("example.md"));
|
||||
}
|
||||
|
||||
void OpenFile()
|
||||
{
|
||||
if(String f = SelectFileOpen("*.md"); !f.IsEmpty())
|
||||
ParseFile(f);
|
||||
}
|
||||
|
||||
void ParseFile(const String& fname)
|
||||
{
|
||||
images.Clear();
|
||||
MarkdownConverter mdc;
|
||||
mdc.WhenImages = [&](VectorMap<String, String>& imglist)
|
||||
{
|
||||
String dir = GetFileDirectory(fname);
|
||||
Progress pi(t_("Loading images..."), imglist.GetCount());
|
||||
pi.pi.Percent();
|
||||
for(int i = 0; i < imglist.GetCount(); i++) {
|
||||
if(String path = NormalizePath(imglist.GetKey(i), dir); FileExists(path)) { // Image resource paths are expected to be relative to the main markdown file's.
|
||||
if(Image img = StreamRaster::LoadFileAny(path); !IsNull(img))
|
||||
imglist[i] << images.Add(CreatePNGObject(img));
|
||||
if(pi.StepCanceled())
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
view.SetQTF(mdc.Tables().NoHtml().ToQtf(LoadFile(fname)));
|
||||
}
|
||||
|
||||
void CopyAsQtf()
|
||||
{
|
||||
AppendClipboardUnicodeText(~view);
|
||||
}
|
||||
};
|
||||
|
||||
GUI_APP_MAIN
|
||||
{
|
||||
MarkdownViewer().Run();
|
||||
}
|
||||
BIN
reference/MarkdownViewer/tux.png
Normal file
BIN
reference/MarkdownViewer/tux.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 89 KiB |
Loading…
Add table
Add a link
Reference in a new issue