diff --git a/Dockerfile b/Dockerfile index 151a497..68cf92f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ -FROM ruby:3.2.0-alpine3.17 -ENV WORKSPACE /rsyntaxtree +FROM ruby:3.3-alpine +ENV WORKSPACE=/rsyntaxtree WORKDIR $WORKSPACE RUN apk update && \ diff --git a/Rakefile b/Rakefile index d9eb1c9..73921d2 100644 --- a/Rakefile +++ b/Rakefile @@ -23,7 +23,7 @@ task :docker_build do `docker build ./ -t rsyntaxtree_devel` end -desc "Generate SVG and PNG example images using Docker mage" +desc "Generate SVG and PNG example images using Docker image" task :docker_generate do docpath = File.expand_path(File.join(__dir__, "docs")) `docker build ./ -t rsyntaxtree_devel` diff --git a/docs/_examples/053.md b/docs/_examples/053.md new file mode 100644 index 0000000..63e2f3e --- /dev/null +++ b/docs/_examples/053.md @@ -0,0 +1,41 @@ +--- +name: "053" +color: "on" +caption: "Escaping square brackets" +category: "Miscellaneous" +polyline: "off" +symmetrization: "off" +connector: "auto" +connector_height: "3.0" +linewidth: "1" +hide_default_connectors: "off" +font: "Noto Serif" +--- +``` +[expr + [id x + + ] + [suffix + \[ + [id 2 + + ] + \] + [suffix + \[ + [id 3 + + ] + \] + [suffix + \[ + [id 4 + + ] + \] + ] + ] + ] +] +``` diff --git a/docs/assets/img/005.png b/docs/assets/img/005.png index e4eef5b..f5d5e3c 100644 Binary files a/docs/assets/img/005.png and b/docs/assets/img/005.png differ diff --git a/docs/assets/img/011.png b/docs/assets/img/011.png index 5fd658f..069a3a5 100644 Binary files a/docs/assets/img/011.png and b/docs/assets/img/011.png differ diff --git a/docs/assets/img/012.png b/docs/assets/img/012.png index 281fd59..ce9d6e1 100644 Binary files a/docs/assets/img/012.png and b/docs/assets/img/012.png differ diff --git a/docs/assets/img/013.png b/docs/assets/img/013.png index 5792d2f..362553f 100644 Binary files a/docs/assets/img/013.png and b/docs/assets/img/013.png differ diff --git a/docs/assets/img/026.png b/docs/assets/img/026.png index c35f308..36e51a9 100644 Binary files a/docs/assets/img/026.png and b/docs/assets/img/026.png differ diff --git a/docs/assets/img/027.png b/docs/assets/img/027.png index 1754792..31d7072 100644 Binary files a/docs/assets/img/027.png and b/docs/assets/img/027.png differ diff --git a/docs/assets/img/033.png b/docs/assets/img/033.png index 460072d..dbcad96 100644 Binary files a/docs/assets/img/033.png and b/docs/assets/img/033.png differ diff --git a/docs/assets/img/040.png b/docs/assets/img/040.png index 2605f26..be80990 100644 Binary files a/docs/assets/img/040.png and b/docs/assets/img/040.png differ diff --git a/docs/assets/img/053.png b/docs/assets/img/053.png new file mode 100644 index 0000000..f6a1c40 Binary files /dev/null and b/docs/assets/img/053.png differ diff --git a/docs/assets/svg/005.svg b/docs/assets/svg/005.svg index e7f9b95..2b7e3cb 100644 --- a/docs/assets/svg/005.svg +++ b/docs/assets/svg/005.svg @@ -1,6 +1,6 @@ - + @@ -24,27 +24,27 @@ -" -X -max +" +X +max -[Y−X] +[Y−X] -YP +YP -Spec -1 +Spec +1 -Y' +Y' -t +t -ZP +ZP - - - - - - + + + + + + \ No newline at end of file diff --git a/docs/assets/svg/011.svg b/docs/assets/svg/011.svg index 7f81a53..d40ec4b 100644 --- a/docs/assets/svg/011.svg +++ b/docs/assets/svg/011.svg @@ -1,6 +1,6 @@ - + @@ -24,35 +24,35 @@ -" -S -< +" +S +< NP > -NP/N -the +NP/N +the N dog -S\NP -> +S\NP +> (S\NP)\NP -bit +bit -NP -John +NP +John - - - + + + - - - + + + - - \ No newline at end of file + + \ No newline at end of file diff --git a/docs/assets/svg/012.svg b/docs/assets/svg/012.svg index 0e1c0b5..90eb731 100644 --- a/docs/assets/svg/012.svg +++ b/docs/assets/svg/012.svg @@ -1,6 +1,6 @@ - + @@ -24,15 +24,15 @@ -" -S -> +" +S +> -S/NP -B -> +S/NP +B +> -S/(S\NP) +S/(S\NP) T > @@ -46,19 +46,19 @@ dog (S\NP)/NP -bit +bit -NP -John +NP +John - - - - + + + + - - \ No newline at end of file + + \ No newline at end of file diff --git a/docs/assets/svg/013.svg b/docs/assets/svg/013.svg index 1681308..82576fe 100644 --- a/docs/assets/svg/013.svg +++ b/docs/assets/svg/013.svg @@ -1,6 +1,6 @@ - + @@ -24,11 +24,11 @@ -" -HEAD -1 -SPR -COMPS +" +HEAD +1 +SPR +COMPS HEAD 2 @@ -38,12 +38,12 @@ Kim -HEAD -1 -SPR -2 - -COMPS +HEAD +1 +SPR +2 + +COMPS HEAD 1 @@ -53,60 +53,60 @@ NP〉 COMPS 3 -PP[ -on -]〉 +PP[ +on +]〉 -relies +relies -■■■■■■ -3 -HEAD -4 -SPR -COMPS +■■■■■■ +3 +HEAD +4 +SPR +COMPS -HEAD -4 -prep -FORM -on -SPR -COMPS -5 - +HEAD +4 +prep +FORM +on +SPR +COMPS +5 + -on +on -HEAD -5 -noun -SPR -COMPS +HEAD +5 +noun +SPR +COMPS -Sandy +Sandy - - + + - - - - - - - + + + + + + + - + + points='313.7874999999999,16.875 305.38749999999993,16.875 305.38749999999993,229.0 313.7874999999999,229.0' /> + points='525.7875,16.875 534.1875,16.875 534.1875,229.0 525.7875,229.0' /> - + + points='618.7749999999999,328.875 610.3749999999999,328.875 610.3749999999999,541.0 618.7749999999999,541.0' /> + points='830.775,328.875 839.175,328.875 839.175,541.0 830.775,541.0' /> - - + + + points='640.0000000000001,640.875 648.4000000000001,640.875 648.4000000000001,945.0 640.0000000000001,945.0' /> - + - + + points='883.55,640.875 875.15,640.875 875.15,945.0 883.55,945.0' /> + points='1095.55,640.875 1103.95,640.875 1103.95,945.0 1095.55,945.0' /> - - + + + points='707.2,1044.875 698.8000000000001,1044.875 698.8000000000001,1396.0 707.2,1396.0' /> + points='968.2000000000002,1044.875 976.6000000000001,1044.875 976.6000000000001,1396.0 968.2000000000002,1396.0' /> - - + + + points='1035.3999999999999,1044.875 1026.9999999999998,1044.875 1026.9999999999998,1349.0 1035.3999999999999,1349.0' /> + points='1247.3999999999996,1044.875 1255.7999999999997,1044.875 1255.7999999999997,1349.0 1247.3999999999996,1349.0' /> \ No newline at end of file diff --git a/docs/assets/svg/026.svg b/docs/assets/svg/026.svg index 0ea3c0f..a896f2e 100644 --- a/docs/assets/svg/026.svg +++ b/docs/assets/svg/026.svg @@ -1,6 +1,6 @@ - + @@ -24,27 +24,27 @@ -" -Reference: +" +Reference: -[situational] -exophora +[situational] +exophora -[textual] -endophora +[textual] +endophora -[toprecedingtext] -anaphora +[toprecedingtext] +anaphora -[tofollowingtext] -cataphora +[tofollowingtext] +cataphora + points='109.30000000000001 159.75 109.30000000000001 127.6875 355.825 127.6875 355.825 95.625' /> + points='602.35 159.75 602.35 127.6875 355.825 127.6875 355.825 95.625' /> + points='420.0 353.25 420.0 321.1875 602.35 321.1875 602.35 289.125' /> + points='784.7 353.25 784.7 321.1875 602.35 321.1875 602.35 289.125' /> \ No newline at end of file diff --git a/docs/assets/svg/027.svg b/docs/assets/svg/027.svg index 2fa852a..9e9af8b 100644 --- a/docs/assets/svg/027.svg +++ b/docs/assets/svg/027.svg @@ -1,6 +1,6 @@ - + @@ -24,78 +24,78 @@ -" -REJOINDER -(anycohesivesequelbydifferentspeaker) +" +REJOINDER +(anycohesivesequelbydifferentspeaker) -(followingaquestion) -RESPONSE +(followingaquestion) +RESPONSE -(answering) -DIRECT -RESPONSE +(answering) +DIRECT +RESPONSE -(notanswering) -INDIRECTRESPONSE +(notanswering) +INDIRECTRESPONSE -(attitudetoanswer) -COMMENTARY +(attitudetoanswer) +COMMENTARY -(evadinganswer) -DISCLAIMER +(evadinganswer) +DISCLAIMER -(implyinganswer) -SUPPLEMENTARY -RESPONSE +(implyinganswer) +SUPPLEMENTARY +RESPONSE -(notfollowingaquestion) -[otherrejoinders] +(notfollowingaquestion) +[otherrejoinders] -(followingastatement) +(followingastatement) -ASSENT +ASSENT -CONTRADICTION +CONTRADICTION -(followinga -statementor -command) -QUESTION -REJOINDER +(followinga +statementor +command) +QUESTION +REJOINDER -(following -acommand) +(following +acommand) -CONSENT +CONSENT -REFUSAL +REFUSAL + points='446.5 272.25 446.5 206.4375 1191.5 206.4375 1191.5 140.625' /> + points='1936.5 272.25 1936.5 206.4375 1191.5 206.4375 1191.5 140.625' /> + points='108.29999999999995 533.25 108.29999999999995 467.4375 446.5 467.4375 446.5 401.625' /> + points='784.7 533.25 784.7 467.4375 446.5 467.4375 446.5 401.625' /> + points='427.0 794.25 427.0 728.4375 784.7 728.4375 784.7 662.625' /> + points='792.1999999999999 794.25 792.1999999999999 728.4375 784.7 728.4375 784.7 662.625' /> + points='1142.4 794.25 1142.4 728.4375 784.7 728.4375 784.7 662.625' /> + points='1545.6999999999998 533.25 1545.6999999999998 467.4375 1936.5 467.4375 1936.5 401.625' /> + points='1978.0 533.25 1978.0 467.4375 1936.5 467.4375 1936.5 401.625' /> + points='2327.2999999999997 533.25 2327.2999999999997 467.4375 1936.5 467.4375 1936.5 401.625' /> + points='1415.1 749.25 1415.1 683.4375 1545.6999999999998 683.4375 1545.6999999999998 617.625' /> + points='1676.3 749.25 1676.3 683.4375 1545.6999999999998 683.4375 1545.6999999999998 617.625' /> + points='2220.7 794.25 2220.7 728.4375 2327.2999999999997 728.4375 2327.2999999999997 662.625' /> + points='2433.8999999999996 794.25 2433.8999999999996 728.4375 2327.2999999999997 728.4375 2327.2999999999997 662.625' /> \ No newline at end of file diff --git a/docs/assets/svg/040.svg b/docs/assets/svg/040.svg index df733dd..40e20c8 100644 --- a/docs/assets/svg/040.svg +++ b/docs/assets/svg/040.svg @@ -1,6 +1,6 @@ - + @@ -24,97 +24,97 @@ -" -root -[cons][sonor] -stricture -[later][strid][contin] +" +root +[cons][sonor] +stricture +[later][strid][contin] -cavity -Oral +cavity +Oral -articulator -Labial +articulator +Labial -terminal -features -[round] +terminal +features +[round] -articulator -Coronal +articulator +Coronal -terminal -features -[anter] -[distrib] +terminal +features +[anter] +[distrib] -articulator -Dorsal +articulator +Dorsal -terminal -features -[back] -[high] -[low] +terminal +features +[back] +[high] +[low] -cavity -Nasal +cavity +Nasal -articulator -SoftPalate +articulator +SoftPalate -terminal -features -[nasal] +terminal +features +[nasal] -cavity -Pharyngeal +cavity +Pharyngeal -articulator -Radical +articulator +Radical -terminal -features -[ATR] -[RTR] +terminal +features +[ATR] +[RTR] -articulator -Glottal +articulator +Glottal -terminal -features -[spreadgl] -[constrgl] -[voiced] +terminal +features +[spreadgl] +[constrgl] +[voiced] + points='369.00000000000006 317.25 369.00000000000006 273.9375 816.3000000000001 273.9375 816.3000000000001 230.625' /> + points='880.2 317.25 880.2 273.9375 816.3000000000001 273.9375 816.3000000000001 230.625' /> + points='1263.6 317.25 1263.6 273.9375 816.3000000000001 273.9375 816.3000000000001 230.625' /> + points='113.39999999999998 533.25 113.39999999999998 489.9375 369.00000000000006 489.9375 369.00000000000006 446.625' /> + points='369.0000000000001 533.25 369.0000000000001 489.9375 369.00000000000006 489.9375 369.00000000000006 446.625' /> + points='624.6000000000001 533.25 624.6000000000001 489.9375 369.00000000000006 489.9375 369.00000000000006 446.625' /> + points='113.40000000000009 749.25 113.40000000000009 705.9375 113.39999999999998 705.9375 113.39999999999998 662.625' /> + points='369.0000000000001 749.25 369.0000000000001 705.9375 369.0000000000001 705.9375 369.0000000000001 662.625' /> + points='624.6000000000001 749.25 624.6000000000001 705.9375 624.6000000000001 705.9375 624.6000000000001 662.625' /> + points='880.2 533.25 880.2 489.9375 880.2 489.9375 880.2 446.625' /> + points='880.2 749.25 880.2 705.9375 880.2 705.9375 880.2 662.625' /> + points='1135.8000000000002 533.25 1135.8000000000002 489.9375 1263.6 489.9375 1263.6 446.625' /> + points='1391.4 533.25 1391.4 489.9375 1263.6 489.9375 1263.6 446.625' /> + points='1135.8000000000002 749.25 1135.8000000000002 705.9375 1135.8000000000002 705.9375 1135.8000000000002 662.625' /> + points='1391.4 749.25 1391.4 705.9375 1391.4 705.9375 1391.4 662.625' /> \ No newline at end of file diff --git a/docs/assets/svg/053.svg b/docs/assets/svg/053.svg new file mode 100644 index 0000000..2b482b6 --- /dev/null +++ b/docs/assets/svg/053.svg @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +" +expr + +id + +x + +suffix + +[ + +id + +2 + +] + +suffix + +[ + +id + +3 + +] + +suffix + +[ + +id + +4 + +] + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/rsyntaxtree.rb b/lib/rsyntaxtree.rb index 0b5b586..dac729b 100755 --- a/lib/rsyntaxtree.rb +++ b/lib/rsyntaxtree.rb @@ -66,8 +66,6 @@ def initialize(params = {}) .gsub('-CABRACKET-', '>') .gsub('¥¥', '\¥') .gsub(/(?", '>').gsub("<", '<') + # エスケープされた角括弧の処理 + text = text.gsub('\\[', '[') + .gsub('\\]', ']') + e[:text] = text.gsub(" ", WHITESPACE_BLOCK) + .gsub(">", '>') + .gsub("<", '<') @contains_phrase = true if text.include?(" ") decoration = e[:decoration] @@ -103,10 +108,10 @@ def setup seg[:char] end this_font = if seg[:type] == :emoji - @fontset[:emoji] - else - font - end + @fontset[:emoji] + else + font + end metrics = FontMetrics.get_metrics(ch, this_font, fontsize, style, weight) width += metrics.width end @@ -119,13 +124,14 @@ def setup width = metrics.width end + # 以下は変更なし if e[:decoration].include?(:box) || e[:decoration].include?(:circle) || e[:decoration].include?(:bar) e[:content_width] = width width += if e[:text].size == 1 - height - width - else - @global[:width_half_x] - end + height - width + else + @global[:width_half_x] + end end if e[:decoration].include?(:whitespace) diff --git a/lib/rsyntaxtree/markup_parser.rb b/lib/rsyntaxtree/markup_parser.rb index 02987e7..e8c55f9 100755 --- a/lib/rsyntaxtree/markup_parser.rb +++ b/lib/rsyntaxtree/markup_parser.rb @@ -14,7 +14,8 @@ class MarkupParser < Parslet::Parser rule(:triangle) { str('^') } rule(:path) { (str('+') >> str('-').maybe >> (str('>') | str('<')).maybe >> match('\d').repeat(1)).as(:path) } - rule(:escaped) { str('\\') >> match('[#<>{}\\^+*_=~\|\n\-]').as(:chr) } + # rule(:escaped) { str('\\') >> match('[#<>{}\\^+*_=~\|\n\-]').as(:chr) } + rule(:escaped) { str('\\') >> match('[#<>{}\\\\^+*_=~\\|\\n\\-\\[\\]]').as(:chr) } rule(:non_escaped) { ((match('[#<>{}\\^+*_=~\|\-]') | str('\\n')).absent? >> any).as(:chr) } rule(:text) { (escaped | non_escaped).repeat(1).as(:text) } diff --git a/lib/rsyntaxtree/string_parser.rb b/lib/rsyntaxtree/string_parser.rb index 5daebf8..a9e518e 100755 --- a/lib/rsyntaxtree/string_parser.rb +++ b/lib/rsyntaxtree/string_parser.rb @@ -112,11 +112,11 @@ def get_next_token escape = false while ((@pos + i) < data.length) && !gottoken - ch = data[@pos + i]; + ch = data[@pos + i] case ch when "[" if escape - token += ch + token += '\\[' # エスケープされた角括弧として保持 escape = false elsif i.positive? gottoken = true @@ -125,7 +125,7 @@ def get_next_token end when "]" if escape - token += ch + token += '\\]' # エスケープされた角括弧として保持 escape = false else token += ch if i.zero? @@ -179,33 +179,41 @@ def make_tree(parent) token_r = token.split(//) case token_r[0] when "[" - tl = token_r.length - token_r = token_r[1, tl - 1] - spaceat = token_r.index(" ") - newparent = -1 - - if spaceat - parts[0] = token_r[0, spaceat].join - - tl = token_r.length - parts[1] = token_r[spaceat, tl - spaceat].join - - element = Element.new(@id, parent, parts[0], @level, @fontset, @fontsize, @global) + # エスケープされた角括弧をチェック + if token =~ /\A\\\[/ || token =~ /\A\\\]/ + # エスケープされた角括弧の場合は通常のテキストとして扱う + element = Element.new(@id, parent, token, @level, @fontset, @fontsize, @global) @id += 1 @elist.add(element) - newparent = element.id - - element = Element.new(@id, @id - 1, parts[1], @level + 1, @fontset, @fontsize, @global) - @id += 1 else - joined = token_r.join - element = Element.new(@id, parent, joined, @level, @fontset, @fontsize, @global) - @id += 1 - newparent = element.id + # 以下、既存の処理 + tl = token_r.length + token_r = token_r[1, tl - 1] + spaceat = token_r.index(" ") + newparent = -1 + + if spaceat + parts[0] = token_r[0, spaceat].join + tl = token_r.length + parts[1] = token_r[spaceat, tl - spaceat].join + + element = Element.new(@id, parent, parts[0], @level, @fontset, @fontsize, @global) + @id += 1 + @elist.add(element) + newparent = element.id + + element = Element.new(@id, @id - 1, parts[1], @level + 1, @fontset, @fontsize, @global) + @id += 1 + else + joined = token_r.join + element = Element.new(@id, parent, joined, @level, @fontset, @fontsize, @global) + @id += 1 + newparent = element.id + end + @elist.add(element) + @level += 1 + make_tree(newparent) end - @elist.add(element) - @level += 1 - make_tree(newparent) else if token.strip != "" element = Element.new(@id, parent, token, @level, @fontset, @fontsize, @global) diff --git a/lib/rsyntaxtree/version.rb b/lib/rsyntaxtree/version.rb index b9d8c96..046b1ce 100755 --- a/lib/rsyntaxtree/version.rb +++ b/lib/rsyntaxtree/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RSyntaxTree - VERSION = "1.3.0" + VERSION = "1.3.1" end diff --git a/test/markup_parser_test.rb b/test/markup_parser_test.rb index 6563aec..0275613 100644 --- a/test/markup_parser_test.rb +++ b/test/markup_parser_test.rb @@ -207,4 +207,11 @@ def test_evaluator Markup.parse text2 # {:status=>:error, :text=>"!^#----\\n\\nX_Y_Z+1+>2"} end + + def test_escaped_brackets + text = "[expr [id x] [suffix \\[ [id 2] \\] ] ]" + @parser = MarkupParser.new + @parser.parse(text) + assert true + end end