Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed attempt to serialize code like HTML <pre> #1936

Draft
wants to merge 6 commits into
base: branch-1.2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TeXmacs/packages/environment/env-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@
<python|<arg|body>>
</pseudo-code>
</macro>>

<drd-props|python-code|prog|true>

<\active*>
<\src-comment>
Expand Down
28 changes: 28 additions & 0 deletions TeXmacs/tests/tmu/66_8_prog.tmu
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<TMU|<tuple|1.0.2|1.2.8-alpha2>>

<style|<tuple|generic|no-page-numbers|chinese>>

<\body>
开始

<\python-code>
import os
def less(a, b):<label|hello>
return a <= b
</python-code>

结束
</body>

<\initial>
<\collection>
<associate|page-medium|paper>
<associate|page-screen-margin|false>
</collection>
</initial>

<\references>
<\collection>
<associate|hello|<tuple|?|?>>
</collection>
</references>
51 changes: 40 additions & 11 deletions src/Data/Convert/Mogan/from_tmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct tmu_reader {
string read_char ();
string read_next ();
string read_function_name ();
string read_prog_line ();
tree read_apply (string s, bool skip_flag);
tree read (bool skip_flag);
};
Expand Down Expand Up @@ -176,6 +177,16 @@ tmu_reader::read_function_name () {
return name;
}

string
tmu_reader::read_prog_line () {
int old_pos= pos;
int buf_N = N (buf);
while (((pos + 1) < buf_N) && (buf[pos] != '\n')) {
pos++;
}
return buf (old_pos, pos);
}

static void
get_collection (tree& u, tree t) {
if (is_func (t, COLLECTION) || is_func (t, DOCUMENT) || is_func (t, CONCAT)) {
Expand All @@ -196,18 +207,36 @@ tmu_reader::read_apply (string name, bool skip_flag) {
t= tree ((tree_label) codes[name]);
}

bool closed= !skip_flag;
int buf_N = N (buf);
while (pos < buf_N) {
// cout << "last= " << last << LF;
bool sub_flag= (skip_flag) && ((last == "") || (last[N (last) - 1] != '|'));
if (sub_flag) (void) skip_blank ();
t << read (sub_flag);
if ((last == "/>") || (last == "/|")) closed= true;
if (closed && ((last == ">") || (last == "/>"))) break;
int buf_N= N (buf);
if (is_tree_in_prog (t)) {
tree D (DOCUMENT);
while (pos < buf_N) {
string line= read_prog_line ();
if (is_empty (line)) {
pos++;
continue;
}
if (ends (line, "</" * name * ">")) {
break;
}
D << line;
}
t << D;
}
else {
bool closed= !skip_flag;
while (pos < buf_N) {
// cout << "last= " << last << LF;
bool sub_flag=
(skip_flag) && ((last == "") || (last[N (last) - 1] != '|'));
if (sub_flag) (void) skip_blank ();
t << read (sub_flag);
if ((last == "/>") || (last == "/|")) closed= true;
if (closed && ((last == ">") || (last == "/>"))) break;
}
// cout << "last= " << last << UNINDENT << LF;
// cout << "Done" << LF;
}
// cout << "last= " << last << UNINDENT << LF;
// cout << "Done" << LF;

if (is_func (t, COLLECTION)) {
tree u (COLLECTION);
Expand Down
40 changes: 39 additions & 1 deletion src/Data/Convert/Mogan/to_tmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct tmu_writer {
void br (int indent= 0);
void tag (string before, string s, string after);
void apply (string func, array<tree> args);
void write_prog (tree t);
void write (tree t);
};

Expand Down Expand Up @@ -208,6 +209,38 @@ tmu_writer::apply (string func, array<tree> args) {
}
}

void
tmu_writer::write_prog (tree t) {
string func= as_string (L (t));

// <\python-code>
write ("<\\", false);
write (func, true, true);
write (">", false);
write ("\n", false);

tree doc = t[0];
int doc_N= N (doc);
for (int i= 0; i < doc_N; i++) {
if (is_atomic (doc[i])) {
write (tree_to_verbatim (doc[i]->label), false);
}
else {
write (doc[i]);
}
write ("\n", false);
}

// __</python-code>
// _ means spaces, the number is controlled by tab
for (int i= 0; i < tab; i++) {
write (" ", false);
}
write ("</", false);
write (func, true, true);
write (">", false);
}

void
tmu_writer::write (tree t) {
if (is_atomic (t)) {
Expand Down Expand Up @@ -265,7 +298,12 @@ tmu_writer::write (tree t) {
tag ("</", as_string (COLLECTION), ">");
break;
default:
apply (as_string (L (t)), A (t));
if (is_tree_in_prog (t) && N (t) == 1 && L (t[0]) == moebius::DOCUMENT) {
write_prog (t);
}
else {
apply (as_string (L (t)), A (t));
}
break;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Data/Convert/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include "analyze.hpp"
#include "hashmap.hpp"
#include "tree.hpp"
#include "tree_helper.hpp"
#include "url.hpp"
#include <moebius/drd/drd_std.hpp>

class object;

Expand Down Expand Up @@ -54,6 +56,11 @@ tree eqnumber_to_nonumber (tree t);
string search_metadata (tree doc, string kind);

/*** TMU ***/
inline bool
is_tree_in_prog (tree t) {
return moebius::drd::the_drd->get_attribute (L (t), "prog") == "true";
}

tree tmu_to_tree (string s);
tree tmu_document_to_tree (string s);
string tree_to_tmu (tree t);
Expand Down
3 changes: 3 additions & 0 deletions src/Typeset/Env/env_exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,9 @@ edit_env_rep::exec_drd_props (tree t) {
else if (prop == "name") {
if (is_atomic (val)) drd->set_attribute (l, prop, val->label);
}
else if (prop == "prog") {
if (is_atomic (val)) drd->set_attribute (l, prop, val->label);
}
else if (prop == "syntax") drd->set_syntax (l, val);
else if (prop == "border") {
if (val == "yes") drd->set_border (l, BORDER_YES);
Expand Down
Loading