Skip to content

Commit f131ba6

Browse files
committed
Add txt export
1 parent 4e8881f commit f131ba6

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

rm_lines/headers/renderer/renderer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ class Renderer {
2828

2929
json getParagraphs() const;
3030

31+
// Exports
3132
void toMd(std::ostream& stream) const;
32-
33+
void toTxt(std::ostream& stream) const;
3334
void toHtml(std::ostream& stream);
3435

3536
private:

rm_lines/headers/renderer/renderer_export.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ EXPORT int destroyRenderer(const char *rendererId);
1414
EXPORT const char *getParagraphs(const char *rendererId);
1515
EXPORT bool textToMdFile(const char *rendererId, const char *outputFile);
1616
EXPORT const char *textToMd(const char *rendererId);
17+
EXPORT bool textToTxtFile(const char *rendererId, const char *outputFile);
18+
EXPORT const char *textToTxt(const char *rendererId);
1719
EXPORT bool textToHtmlFile(const char *rendererId, const char *outputFile);
1820
EXPORT const char *textToHtml(const char *rendererId);
1921

rm_lines/src/renderer/renderer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ void Renderer::toMd(std::ostream &stream) const {
128128
}
129129
}
130130

131+
void Renderer::toTxt(std::ostream &stream) const {
132+
for (const auto &paragraph : textDocument.paragraphs) {
133+
for (const auto &formattedText : paragraph.contents) {
134+
// Write the formatted text
135+
stream << formattedText.text;
136+
}
137+
stream << "\n"; // Add newline after each paragraph
138+
}
139+
}
140+
131141
void Renderer::toHtml(std::ostream &stream) {
132142
stream << HTML_HEADER;
133143
// TODO: Implement HTML rendering based on rM rendering, textDocument and *GliphRange(s)* for markings on the text!

rm_lines/src/renderer/renderer_export.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,38 @@ const char * textToMd(const char *rendererId) {
109109

110110
return result.c_str();
111111
}
112+
bool textToTxtFile(const char *rendererId, const char *outputFile) {
113+
const auto renderer = getRenderer(rendererId);
114+
if (!renderer) {
115+
logError("Invalid treeId provided");
116+
return false;
117+
}
118+
119+
std::ofstream fileStream(outputFile);
120+
if (!fileStream) {
121+
logError(std::format("Failed to open file: {}", outputFile));
122+
return false;
123+
}
124+
renderer->toTxt(fileStream);
125+
return true;
126+
}
127+
128+
129+
const char * textToTxt(const char *rendererId) {
130+
const auto renderer = getRenderer(rendererId);
131+
if (!renderer) {
132+
logError("Invalid treeId provided");
133+
return "";
134+
}
135+
136+
static std::string result;
137+
std::ostringstream stringStream;
138+
139+
renderer->toTxt(stringStream);
140+
result = stringStream.str();
141+
142+
return result.c_str();
143+
}
112144

113145
bool textToHtmlFile(const char *rendererId, const char *outputFile) {
114146
const auto renderer = getRenderer(rendererId);

rm_lines_sys/src/rm_lines_sys/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def textToMdFile(self, renderer_id: bytes, md_file: bytes) -> bool:
3636
def textToMd(self, renderer_id: bytes) -> bytes:
3737
pass
3838

39+
def textToTxtFile(self, renderer_id: bytes, md_file: bytes) -> bool:
40+
pass
41+
42+
def textToTxt(self, renderer_id: bytes) -> bytes:
43+
pass
44+
3945
def textToHtmlFile(self, renderer_id: bytes, html_file: bytes) -> bool:
4046
pass
4147

@@ -107,6 +113,14 @@ def load_lib() -> Optional[ctypes.CDLL]:
107113
_lib.textToMd.argtypes = [ctypes.c_char_p]
108114
_lib.textToMd.restype = ctypes.c_char_p
109115

116+
# Function textToTxtFile(str, str) -> bool
117+
_lib.textToTxtFile.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
118+
_lib.textToTxtFile.restype = ctypes.c_bool
119+
120+
# Function textToTxt(str) -> str
121+
_lib.textToTxt.argtypes = [ctypes.c_char_p]
122+
_lib.textToTxt.restype = ctypes.c_char_p
123+
110124
# Function textToHtmlFile(str, str) -> bool
111125
_lib.textToHtmlFile.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
112126
_lib.textToHtmlFile.restype = ctypes.c_bool

0 commit comments

Comments
 (0)