modify some function

This commit is contained in:
Tan, Kian-ting 2021-06-28 00:22:37 +08:00
parent fe20b8e21f
commit 932f596f08
7 changed files with 147 additions and 75 deletions

View file

@ -1,6 +1,7 @@
[def-syntax q [[_] [str "\""]]] [def-syntax q [[_] [str "\""]]]
[script "packages/font-fallback"] [script "packages/font-fallback"]
[script "packages/grid"] [script "packages/grid"]
[script "packages/image"]
[define section-var 1] [define section-var 1]
[define sub-section-var 1] [define sub-section-var 1]
@ -8,7 +9,7 @@
% custom macro % custom macro
[def-syntax section [def-syntax section
[[_ x][begin [[_ x][str-append-many
[set! section-title [str-append [str-append [str section-var] [str ". " ]] [str x]]] [set! section-title [str-append [str-append [str section-var] [str ". " ]] [str x]]]
[font [[size "20pt"][weight "800"]] section-title] [font [[size "20pt"][weight "800"]] section-title]
[set! section-var [+ section-var 1]] [set! section-var [+ section-var 1]]
@ -19,7 +20,7 @@
[def-syntax subsection [def-syntax subsection
[[_ x][begin [[_ x][str-append-many
[set! section-title [str-append [str-append [str sub-section-var] [str ". " ]] [str x]]] [set! section-title [str-append [str-append [str sub-section-var] [str ". " ]] [str x]]]
[font [[size "16pt"][weight "600"]] section-title] [font [[size "16pt"][weight "600"]] section-title]
[set! sub-section-var [+ sub-section-var 1]] [set! sub-section-var [+ sub-section-var 1]]
@ -55,15 +56,40 @@ The functions that it has (although may be buggy or needed to be tested) is:
[section "Simple manual"] [section "Simple manual"]
[subsection "Editor interface"]
[call img [[src "total-interface.png"] [height "200px"]]]
[subsection "Arimetic calculation and variable and function definition"]
[subsection "Special character"]
[subsection "Font formatting"]
[subsection "Call SILE function and packages"]
[subsection "Macro"] [subsection "Macro"]
Using 'def-syntax', you can add your macro to sile. the example is a "custom-section" macro: Using [font-family "Noto Sans Mono CJK TC" "def-syntax"], you can add your macro to sile. the example is a "custom-section" macro:
[font-family "Noto Sans Mono CJK TC" "123
124"]
underline [call noindent][font-family "Noto Sans Mono CJK TC" "[def-syntax subsection"]
[call noindent][font-family "Noto Sans Mono CJK TC" "[[_ x][str-append"]
[call noindent][font-family "Noto Sans Mono CJK TC" "[set! section-title [str-append [str-append [str sub-section-var] [str \". \" ]] [str x]]]"]
[call noindent][font-family "Noto Sans Mono CJK TC" "[font [[size \"16pt\"][weight \"600\"]] section-title]"]
[call noindent][font-family "Noto Sans Mono CJK TC" "set! sub-section-var [+ sub-section-var 1]]]]]"]
[str "underline
bold bold
italic italic
font-size font-size
@ -80,7 +106,7 @@ script
docu-para docu-para
call call
xml-to-string xml-to-string
SILE-STRING-ADD!
SILE SILE
print print
set! set!
@ -88,17 +114,4 @@ def-syntax
define function, closure, lambda define function, closure, lambda
str str
str-append str-append
str-append-many" ]]
]
% custom macro
%[set! section-title [str-append [str-append [str [str \"section-var\"]] [str \". \" ]] [str x]]]
%[font [[size \"20pt\"][weight \"800\"]] [str \"section-title\"]]
%[set! [str \"section-var\"] [+ [str \"section-var\"] 1]]

BIN
example/total-interface.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

View file

@ -42,11 +42,11 @@ class ClochurLexer(QsciLexerCustom):
operator_list = [ '-', '+', '*', '/', '>' ,'=','<','>=','<='] operator_list = [ '-', '+', '*', '/', '>' ,'=','<','>=','<=']
# SILE and SILE-STRING-ADD! is internal, so they're not added. # SILE and SILE-STRING-ADD! is internal, so they're not added.
function_list = ['if', 'docu', 'docu-para', 'script', 'call','xml-to-string', 'begin', function_list = ['if', 'docu', 'docu-para', 'script', 'call','xml-to-string', 'begin',
'str','str-append','set!','print', 'define', 'def-syntax', 'lambda', 'eval','cons', 'str','str-append', 'str-append-many','set!','print', 'define', 'def-syntax', 'lambda', 'eval','cons',
'car','cdr','ls-ref','ls'] 'car','cdr','ls-ref','ls']
self.PRIMARY = macro_list + boolean_list + operator_list + function_list self.PRIMARY = macro_list + boolean_list + operator_list + function_list
self.split_pattern = re.compile(r'(\s+|\\%|%|\\\[|\\\]|[[]|[]])') self.split_pattern = re.compile(r'(\s+|\\%|%|\\\[|\\\]|[[]|[]]|\\\"|\")')
font = QFont() font = QFont()
font.setFamily(parent.font_family) font.setFamily(parent.font_family)
@ -196,7 +196,6 @@ class ClochurLexer(QsciLexerCustom):
if rainbow_state >= 10: if rainbow_state >= 10:
new_state = self.String new_state = self.String
elif rainbow_state < 7: elif rainbow_state < 7:
print("rainbow_state" + str(type(rainbow_state)) + str(rainbow_state))
new_state = getattr(self, "Rainbow" + str(rainbow_state)) new_state = getattr(self, "Rainbow" + str(rainbow_state))
rainbow_state = (rainbow_state + 1) % 7 rainbow_state = (rainbow_state + 1) % 7
else: else:

View file

@ -31,10 +31,15 @@ class Interpreter:
[[_ x] [SILE[docu-aux x]]] [[_ x] [SILE[docu-aux x]]]
[[_ x y...] [SILE[docu-aux x y...]]]] [[_ x y...] [SILE[docu-aux x y...]]]]
[def-syntax docu-aux [def-syntax docu-aux
[[_ x] [SILE-STRING-ADD![str x]]] [[_ x] [str x]]
[[_ [x...]] [SILE-STRING-ADD![str [x...]]]] [[_ [x...]] [str [x...]]]
[[_ x y...] [begin[docu-aux x] [docu-aux y...]]]] [[_ x y...] [str-append [docu-aux x] [docu-aux y...]]]]
[def-syntax str-append-many
[[_ x y] [str-append x y]]
[[_ x y z...] [str-append x [str-append-many y z...]]]]
[def-syntax font [def-syntax font
[[_ [para...] inner] [call font [para...] inner]] [[_ [para...] inner] [call font [para...] inner]]
@ -77,6 +82,8 @@ class Interpreter:
def remove_spaces_and_newlines_aux(self, sexp, is_inside_defstx): def remove_spaces_and_newlines_aux(self, sexp, is_inside_defstx):
if isinstance(sexp, list): if isinstance(sexp, list):
result = [] result = []
if sexp == []:
return sexp
if isinstance(sexp[0], dict) and sexp[0]["token"] == "def-syntax": if isinstance(sexp[0], dict) and sexp[0]["token"] == "def-syntax":
is_inside_defstx = True is_inside_defstx = True
if isinstance(sexp[0], dict) and sexp[0]["token"] == "docu" \ if isinstance(sexp[0], dict) and sexp[0]["token"] == "docu" \
@ -365,6 +372,7 @@ class Interpreter:
script_xml = ET.Element('script') script_xml = ET.Element('script')
script_xml.attrib["src"] = self.destring(sexp[1]["token"]) script_xml.attrib["src"] = self.destring(sexp[1]["token"])
self.silexml.append(script_xml) self.silexml.append(script_xml)
return ""
elif sexp[0]["token"] == "docu-para": elif sexp[0]["token"] == "docu-para":
if not len(sexp) == 2: if not len(sexp) == 2:
@ -375,29 +383,42 @@ class Interpreter:
attrib_name = i[0]["token"] attrib_name = i[0]["token"]
attrib_value = self.destring(i[1]["token"]) attrib_value = self.destring(i[1]["token"])
self.silexml.attrib[attrib_name] = attrib_value self.silexml.attrib[attrib_name] = attrib_value
return ""
# [call callee {[[attr1 val1] [attr2 val2] ...]} {inner_val}] # [call callee {[[attr1 val1] [attr2 val2] ...]} {inner_val}]
elif sexp[0]["token"] == "call": elif sexp[0]["token"] == "call":
callee = sexp[1]["token"] callee = sexp[1]["token"]
call_xml = ET.Element(callee) call_xml = ET.Element(callee)
if len(sexp) == 4 or (len(sexp) == 3 and isinstance(sexp[2], list)): if len(sexp) == 4 or (len(sexp) == 3 and isinstance(sexp[2], list) and isinstance(sexp[2][0], list)):
for i in sexp[2]: for i in sexp[2]:
attrib_name = i[0]["token"] if i == []:
attrib_value = self.destring(self.interprete_aux(i[1])) pass
call_xml.attrib[attrib_name] = attrib_value else:
attrib_name = i[0]["token"]
attrib_value = self.destring(self.interprete_aux(i[1]))
call_xml.attrib[attrib_name] = attrib_value
if len(sexp) == 4: if len(sexp) == 4:
call_xml.text = self.destring(self.interprete_aux(sexp[3])) inner_text = self.destring(self.interprete_aux(sexp[3]))
call_xml_text = ET.tostring(call_xml, encoding='unicode')
matched = re.match(r"<("+callee+")([ ]*.*[ ]*)/>", call_xml_text)
call_xml_head = "<" + matched.group(1) + matched.group(2) + ">"
call_xml_tail = "</" + matched.group(1) + ">"
call_xml_text = call_xml_head + inner_text + call_xml_tail
self.silexml.append(call_xml) if len(sexp) == 3:
return SubXMLElement(call_xml) call_xml_text = ET.tostring(call_xml,encoding='unicode')
return call_xml_text
elif len(sexp) == 3: elif len(sexp) == 3:
call_xml.text = self.destring(self.interprete_aux(sexp[2])) inner_text = self.destring(self.interprete_aux(sexp[2]))
self.silexml.append(call_xml) call_xml_head = "<"+callee+">"
return SubXMLElement(call_xml) call_xml_tail = "</"+callee+">"
call_xml_text = call_xml_head + inner_text + call_xml_tail
return call_xml_text
elif len(sexp) == 2: elif len(sexp) == 2:
self.silexml.append(call_xml) return ET.tostring(call_xml, encoding='unicode')
return SubXMLElement(call_xml)
else: else:
raise Exception("Line %d, Col. %d, the form of call is mal-formed." % (sexp[0]["line"], sexp[0]["col"])) raise Exception("Line %d, Col. %d, the form of call is mal-formed." % (sexp[0]["line"], sexp[0]["col"]))
@ -455,36 +476,43 @@ class Interpreter:
# if it's a sub-xml-element, show the string form of it, or return the input unchanged. # if it's a sub-xml-element, show the string form of it, or return the input unchanged.
# It's recommended to use it only print it in terminal with 'print' # It's recommended to use it only print it in terminal with 'print'
elif sexp[0]["token"] == "xml-to-string": #elif sexp[0]["token"] == "xml-to-string":
if len(sexp) != 2: # if len(sexp) != 2:
raise Exception("Line %d, Col. %d, the argument of SHOW-XML-TREE is mal-formed" % (sexp[0]["line"], sexp[0]["col"])) # raise Exception("Line %d, Col. %d, the argument of SHOW-XML-TREE is mal-formed" % (sexp[0]["line"], sexp[0]["col"]))
else: # else:
res = self.interprete_aux(sexp[1]) # res = self.interprete_aux(sexp[1])
if isinstance(res, SubXMLElement): # if isinstance(res, SubXMLElement):
return ET.tostring(res.element, encoding='unicode') # return ET.tostring(res.element, encoding='unicode')
else: # else:
return res # return res
# append string to <sile> # append string to <sile>
elif sexp[0]["token"] == "SILE-STRING-ADD!": #elif sexp[0]["token"] == "SILE-STRING-ADD!":
subelements_found = [x for x in self.silexml.iter() if x != self.silexml] # subelements_found = [x for x in self.silexml.iter() if x != self.silexml]
if subelements_found: # if subelements_found:
if subelements_found[-1].tail == None: # if subelements_found[-1].tail == None:
subelements_found[-1].tail = self.interprete_aux(sexp[1]) # subelements_found[-1].tail = self.interprete_aux(sexp[1])
else: # else:
subelements_found[-1].tail += self.interprete_aux(sexp[1]) # subelements_found[-1].tail += self.interprete_aux(sexp[1])
else: # else:
if self.silexml.text == None: # if self.silexml.text == None:
self.silexml.text = self.interprete_aux(sexp[1]) # self.silexml.text = self.interprete_aux(sexp[1])
else: # else:
self.silexml.text += self.interprete_aux(sexp[1]) # self.silexml.text += self.interprete_aux(sexp[1])
elif sexp[0]["token"] == "SILE": elif sexp[0]["token"] == "SILE":
inner = self.interprete_aux(sexp[1]) inner = self.interprete_aux(sexp[1])
return ET.tostring(self.silexml, encoding="unicode") orig_sile_xml = ET.tostring(self.silexml, encoding="unicode")
matched = re.match(r'(<sile(?:.*)>.*)(</sile>)', orig_sile_xml)
sile_before = matched.group(1)
sile_after = matched.group(2)
sile_xml_inserted = sile_before + inner + sile_after
return sile_xml_inserted
# applying function
else: else:
ret = self.apply(sexp) ret = self.apply(sexp)
return ret return ret

View file

@ -131,9 +131,14 @@ class Window(QMainWindow):
self.about_action.triggered.connect(self.about_call) self.about_action.triggered.connect(self.about_call)
self.bold_action = QAction(QIcon(":text-bold.svg"), "&Bold", self) self.bold_action = QAction(QIcon(":text-bold.svg"), "&Bold", self)
self.bold_action.triggered.connect(self.bold_call)
self.italic_action = QAction(QIcon(":text-italic.svg"), "&Italic", self) self.italic_action = QAction(QIcon(":text-italic.svg"), "&Italic", self)
self.strike_action = QAction(QIcon(":text-strikethrough.svg"), "Stri&ke", self) self.italic_action.triggered.connect(self.italic_call)
self.underline_action = QAction(QIcon(":text-underline.svg"), "&Underline", self) self.underline_action = QAction(QIcon(":text-underline.svg"), "&Underline", self)
self.underline_action.triggered.connect(self.underline_call)
def _createMenuBar(self): def _createMenuBar(self):
menuBar = QMenuBar(self) menuBar = QMenuBar(self)
@ -192,6 +197,15 @@ class Window(QMainWindow):
os.system('clochur') os.system('clochur')
def open_call(self): def open_call(self):
open_file = False
if self.editor.isModified():
reply = QMessageBox.question(self,'','Do You want to save this file before open a file? The text has been modified',
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.No)
if reply == QMessageBox.Yes:
self.save_as_call()
else:
pass
file_path = QFileDialog.getOpenFileName(self, 'Open file...', self.opened_file_dirname, "CLC typesetting format (*.clc)") file_path = QFileDialog.getOpenFileName(self, 'Open file...', self.opened_file_dirname, "CLC typesetting format (*.clc)")
if file_path[0] != '': if file_path[0] != '':
self.filename = os.path.basename(file_path[0]) self.filename = os.path.basename(file_path[0])
@ -342,6 +356,23 @@ class Window(QMainWindow):
def select_all_call(self): def select_all_call(self):
self.editor.selectAll() self.editor.selectAll()
def bold_call(self):
selected_text = self.editor.selectedText()
self.editor.replaceSelectedText(f'[bold {selected_text}]')
def italic_call(self):
selected_text = self.editor.selectedText()
self.editor.replaceSelectedText(f'[italic {selected_text}]')
def underline_call(self):
selected_text = self.editor.selectedText()
self.editor.replaceSelectedText(f'[underline {selected_text}]')
def add_font_call(self):
selected_text = self.editor.selectedText()
font_family = self.font_combo_box.currentText()
self.editor.replaceSelectedText(f'[font-family "{font_family}" {selected_text}]')
def about_call(self): def about_call(self):
@ -394,19 +425,20 @@ class Window(QMainWindow):
'''create font adder''' '''create font adder'''
self.font_widget = QHBoxLayout() self.font_widget = QHBoxLayout()
font_combo_box = QComboBox() self.font_combo_box = QComboBox()
font_database = QFontDatabase() font_database = QFontDatabase()
font_families = font_database.families() font_families = font_database.families()
font_combo_box.addItems(font_families) self.font_combo_box.addItems(font_families)
line_edit = font_combo_box.lineEdit() line_edit = self.font_combo_box.lineEdit()
#line_edit.setFont(QFont(font_combo_box.currentText(),11)) #line_edit.setFont(QFont(font_combo_box.currentText(),11))
#print(type(font_combo_box.lineEdit()).__name__) #print(type(font_combo_box.lineEdit()).__name__)
font_button = QPushButton("Insert font") self.font_button = QPushButton("Insert font")
formatToolBar.addWidget(font_combo_box) formatToolBar.addWidget(self.font_combo_box)
formatToolBar.addWidget(font_button) formatToolBar.addWidget(self.font_button)
self.font_button.clicked.connect(self.add_font_call)
def generate_untitled_title(self): def generate_untitled_title(self):
json_file = os.path.join(self.tmp_folder, self.tmp_file) json_file = os.path.join(self.tmp_folder, self.tmp_file)

Binary file not shown.

View file

@ -227,15 +227,15 @@ See https://github.com/adobe-type-tools/cmap-resources
<span data-l10n-id="presentation_mode_label">Presentation Mode</span> <span data-l10n-id="presentation_mode_label">Presentation Mode</span>
</button> </button>
<button id="openFile" class="toolbarButton openFile hiddenLargeView" title="Open File" tabindex="32" data-l10n-id="open_file"> <button id="openFile" class="toolbarButton openFile hiddenLargeView" title="Open File" tabindex="32" data-l10n-id="open_file">
<span data-l10n-id="open_file_label">Open</span> <span data-l10n-id="open_file_label">Open</span>
</button> </button>
<button id="print" class="toolbarButton print hiddenMediumView" title="Print" tabindex="33" data-l10n-id="print"> <button id="print" class="toolbarButton print hiddenMediumView" title="Print" tabindex="33" data-l10n-id="print">
<span data-l10n-id="print_label">Print</span> <span data-l10n-id="print_label">Print</span>
</button> </button>
<button id="download" class="toolbarButton download hiddenMediumView" title="Download" tabindex="34" data-l10n-id="download"> <button id="download" class="toolbarButton download hiddenMediumView" title="Download" tabindex="34" data-l10n-id="download">
<span data-l10n-id="download_label">Download</span> <span data-l10n-id="download_label">Download</span>
</button> </button>
<a href="#" id="viewBookmark" class="toolbarButton bookmark hiddenSmallView" title="Current view (copy or open in new window)" tabindex="35" data-l10n-id="bookmark"> <a href="#" id="viewBookmark" class="toolbarButton bookmark hiddenSmallView" title="Current view (copy or open in new window)" tabindex="35" data-l10n-id="bookmark">