2021-02-08 14:24:54 +01:00
# test_lib_src2md.rb
2022-04-13 13:33:18 +02:00
require 'minitest/autorun'
require 'fileutils'
require 'diff/lcs'
2021-02-08 14:24:54 +01:00
require_relative '../lib/lib_src2md.rb'
2022-04-16 08:46:29 +02:00
include Math
2022-04-13 13:33:18 +02:00
module Prepare_test
2022-04-16 08:46:29 +02:00
def files_src2md
2022-04-13 13:33:18 +02:00
# Many kinds of file types are supported.
# .c => C, .h => C, .rb => ruby, Rakefile, => ruby, .xml => xml, .ui => xml, .y => bison, .lex => lex, .build => meson, .md => markdown, Makefile => makefile
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_c = << ~ 'EOS'
/ * This comment is very long, longer than 100. It must be folded if this file is converted to latex. Because latex doesn't care about verbatim lines. The lines are printed as they are. * /
#include <stdio.h>
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
void
printf_something ( char * something ) {
printf ( " %s \n " , something ) ;
}
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
int
main ( int argc , char ** argv ) {
printf_something ( " Hello world. " ) ;
}
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_h = << ~ 'EOS'
#include <gtk/gtk.h>
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_rb = << ~ 'EOS'
print " Hello ruby. \n "
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
rakefile = << ~ 'EOS'
task default : " a.out "
file " a.out " = > " sample.c " do
sh " gcc sample.c "
end
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_xml = << ~ 'EOS'
< div class = " h1 " color = " red " > Fatal error! ! < / div>
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_ui = << ~ 'EOS'
< object class = " GtkWindow " id = " win " > < / object>
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_y = << ~ 'EOS'
program : statement | program statement ;
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_lex = << ~ 'EOS'
[ a - zA - Z0 - 9 ] return ALNUM ;
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_build = << ~ 'EOS'
project ( 'sampe' , 'c' )
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample0_md = << ~ 'EOS'
# Sample text in maridown language
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
makefile = << ~ 'EOS'
a . out : sample . c
cc sample . c
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_src_md = << ~ 'EOS'
# Sample.src.md
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
This . src . md file is made for the test for lib_src2md . rb .
2021-02-08 14:24:54 +01:00
2022-04-16 08:46:29 +02:00
No conversion happens .
@ @@include
@ @ @
2022-04-13 13:33:18 +02:00
Sample . c with line number is :
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@include
sample . c
@ @ @
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The following is also Sample . c with line number .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@include - n
sample . c
@ @ @
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The following is Sample . c without line number .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@include - N
sample . c
@ @ @
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The following prints only ` printf_something ` .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@include
sample . c printf_something
@ @ @
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The following prints ` printf_something ` and ` main ` .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@include
sample . c printf_something main
@ @ @
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
Check info string .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@include
sample . c
sample . h
sample . rb
Rakefile
sample . xml
sample . ui
sample . y
sample . lex
sample . build
sample0 . md
@ @ @
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
Compile sample . c with rake like this :
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@shell
rake
@ @ @
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@shell
echo " This text is very long, longer than 100. It must be folded if this file is converted to latex. Because latex doesn't care about verbatim lines. The lines are printed as they are. "
@ @ @
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The target type is :
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
@ @@if gfm
gfm
@ @@elif html
html
@ @@elif latex
latex
@ @@end
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
[ Relative link ] ( sample . c ) will be converted when the target type is gfm or html .
Otherwise ( latex ) the link will be removed .
2021-02-08 14:24:54 +01:00
2022-04-25 03:33:47 +02:00
Another [ relative link ] ( .. / Rakefile ) .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
[ Absolute link ] ( https : / / github . com / ToshioCP ) is kept as it is .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
Image link .
If the target type is gfm or html , the size will be removed .
Otherwise ( latex ) it remains .
2021-02-08 14:24:54 +01:00
2022-04-25 03:33:47 +02:00
! [ Screenshot of the box ] ( .. / image / screenshot_lb4 . png ) { width = 6 . 3 cm height = 5 . 325 cm }
2022-04-13 13:33:18 +02:00
EOS
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
sample_md_gfm = << ~ EOS
# Sample.src.md
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
This . src . md file is made for the test for lib_src2md . rb .
2021-02-08 14:24:54 +01:00
2022-04-16 08:46:29 +02:00
No conversion happens .
@ @@include
@ @ @
2022-04-13 13:33:18 +02:00
Sample . c with line number is :
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ C
#{i=1;sample_c.lines.each {|line| line.replace(sprintf("%#2d %s", i, line)); i+=1}.join}~~~
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The following is also Sample . c with line number .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ C
#{i=1;sample_c.lines.each {|line| line.replace(sprintf("%#2d %s", i, line)); i+=1}.join}~~~
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The following is Sample . c without line number .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ C
#{sample_c}~~~
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The following prints only ` printf_something ` .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ C
1 void
2 printf_something ( char * something ) {
3 printf ( " %s \\ n " , something ) ;
4 }
~ ~ ~
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The following prints ` printf_something ` and ` main ` .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ C
1 void
2 printf_something ( char * something ) {
3 printf ( " %s \\ n " , something ) ;
4 }
5 #{' '}
6 int
7 main ( int argc , char ** argv ) {
8 printf_something ( " Hello world. " ) ;
9 }
~ ~ ~
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
Check info string .
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ C
#{i=1;sample_c.lines.each {|line| line.replace(sprintf("%#2d %s", i, line)); i+=1}.join}~~~
~ ~ ~ C
#{i=1;sample_h.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
~ ~ ~ ruby
#{i=1;sample_rb.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
~ ~ ~ ruby
#{i=1;rakefile.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
~ ~ ~ xml
#{i=1;sample_xml.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
~ ~ ~ xml
#{i=1;sample_ui.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
~ ~ ~ bison
#{i=1;sample_y.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
~ ~ ~ lex
#{i=1;sample_lex.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
~ ~ ~ meson
#{i=1;sample_build.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
~ ~ ~ markdown
#{i=1;sample0_md.lines.each {|line| line.replace(sprintf("%#1d %s", i, line)); i+=1}.join}~~~
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
Compile sample . c with rake like this :
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~
$ rake
~ ~ ~
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~
$ echo " This text is very long, longer than 100. It must be folded if this file is converted to latex. Because latex doesn't care about verbatim lines. The lines are printed as they are. "
This text is very long , longer than 100 . It must be folded if this file is converted to latex . Because latex doesn ' t care about verbatim lines . The lines are printed as they are .
~ ~ ~
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
The target type is :
2021-02-08 14:24:54 +01:00
2022-04-13 13:33:18 +02:00
gfm
2021-03-03 15:45:54 +01:00
2022-04-25 03:33:47 +02:00
[ Relative link ] ( .. / src / sample . c ) will be converted when the target type is gfm or html .
2022-04-13 13:33:18 +02:00
Otherwise ( latex ) the link will be removed .
2021-03-03 15:45:54 +01:00
2022-04-25 03:33:47 +02:00
Another [ relative link ] ( .. / Rakefile ) .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
[ Absolute link ] ( https : / / github . com / ToshioCP ) is kept as it is .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
Image link .
If the target type is gfm or html , the size will be removed .
Otherwise ( latex ) it remains .
2021-03-03 15:45:54 +01:00
2022-04-25 03:33:47 +02:00
! [ Screenshot of the box ] ( .. / image / screenshot_lb4 . png )
2022-04-13 13:33:18 +02:00
EOS
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
sample_md_html = << ~ EOS
# Sample.src.md
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
This . src . md file is made for the test for lib_src2md . rb .
2021-03-03 15:45:54 +01:00
2022-04-16 08:46:29 +02:00
No conversion happens .
@ @@include
@ @ @
2022-04-13 13:33:18 +02:00
Sample . c with line number is :
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ { . C . numberLines }
#{sample_c}~~~
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
The following is also Sample . c with line number .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ { . C . numberLines }
#{sample_c}~~~
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
The following is Sample . c without line number .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ { . C }
#{sample_c}~~~
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
The following prints only ` printf_something ` .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ { . C . numberLines }
void
printf_something ( char * something ) {
printf ( " %s \\ n " , something ) ;
}
~ ~ ~
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
The following prints ` printf_something ` and ` main ` .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ { . C . numberLines }
void
printf_something ( char * something ) {
printf ( " %s \\ n " , something ) ;
}
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
int
main ( int argc , char ** argv ) {
printf_something ( " Hello world. " ) ;
}
~ ~ ~
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
Check info string .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ { . C . numberLines }
#{sample_c}~~~
~ ~ ~ { . C . numberLines }
#{sample_h}~~~
~ ~ ~ { . ruby . numberLines }
#{sample_rb}~~~
~ ~ ~ { . ruby . numberLines }
#{rakefile}~~~
~ ~ ~ { . xml . numberLines }
#{sample_xml}~~~
~ ~ ~ { . xml . numberLines }
#{sample_ui}~~~
~ ~ ~ { . bison . numberLines }
#{sample_y}~~~
~ ~ ~ { . lex . numberLines }
#{sample_lex}~~~
~ ~ ~ { . numberLines }
#{sample_build}~~~
~ ~ ~ { . markdown . numberLines }
#{sample0_md}~~~
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
Compile sample . c with rake like this :
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~
$ rake
~ ~ ~
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~
$ echo " This text is very long, longer than 100. It must be folded if this file is converted to latex. Because latex doesn't care about verbatim lines. The lines are printed as they are. "
This text is very long , longer than 100 . It must be folded if this file is converted to latex . Because latex doesn ' t care about verbatim lines . The lines are printed as they are .
~ ~ ~
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
The target type is :
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
html
2021-03-03 15:45:54 +01:00
2022-04-22 05:04:27 +02:00
Relative link will be converted when the target type is gfm or html .
2022-04-13 13:33:18 +02:00
Otherwise ( latex ) the link will be removed .
2021-03-03 15:45:54 +01:00
2022-04-22 05:04:27 +02:00
Another relative link .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
[ Absolute link ] ( https : / / github . com / ToshioCP ) is kept as it is .
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
Image link .
If the target type is gfm or html , the size will be removed .
Otherwise ( latex ) it remains .
2021-03-03 15:45:54 +01:00
2022-04-22 05:04:27 +02:00
! [ Screenshot of the box ] ( image / screenshot_lb4 . png )
2022-04-13 13:33:18 +02:00
EOS
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
sample_md_latex = << ~ EOS
# Sample.src.md
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
This . src . md file is made for the test for lib_src2md . rb .
2021-03-03 15:45:54 +01:00
2022-04-16 08:46:29 +02:00
No conversion happens .
@ @@include
@ @ @
2022-04-13 13:33:18 +02:00
Sample . c with line number is :
2021-03-03 15:45:54 +01:00
2022-04-13 13:33:18 +02:00
~ ~ ~ { . C . numberLines }
#{sample_c}~~~
The following is also Sample . c with line number .
~ ~ ~ { . C . numberLines }
#{sample_c}~~~
The following is Sample . c without line number .
~ ~ ~ { . C }
#{sample_c}~~~
The following prints only ` printf_something ` .
~ ~ ~ { . C . numberLines }
void
printf_something ( char * something ) {
printf ( " %s \\ n " , something ) ;
}
~ ~ ~
The following prints ` printf_something ` and ` main ` .
~ ~ ~ { . C . numberLines }
void
printf_something ( char * something ) {
printf ( " %s \\ n " , something ) ;
}
int
main ( int argc , char ** argv ) {
printf_something ( " Hello world. " ) ;
}
~ ~ ~
Check info string .
~ ~ ~ { . C . numberLines }
#{sample_c}~~~
~ ~ ~ { . C . numberLines }
#{sample_h}~~~
~ ~ ~ { . ruby . numberLines }
#{sample_rb}~~~
~ ~ ~ { . ruby . numberLines }
#{rakefile}~~~
~ ~ ~ { . xml . numberLines }
#{sample_xml}~~~
~ ~ ~ { . xml . numberLines }
#{sample_ui}~~~
~ ~ ~ { . numberLines }
#{sample_y}~~~
~ ~ ~ { . numberLines }
#{sample_lex}~~~
~ ~ ~ { . numberLines }
#{sample_build}~~~
~ ~ ~ { . numberLines }
#{sample0_md}~~~
Compile sample . c with rake like this :
~ ~ ~
$ rake
~ ~ ~
~ ~ ~
$ echo " This text is very long, longer than 100. It must be folded if this file is converted to latex. Because latex doesn't care about verbatim lines. The lines are printed as they are. "
This text is very long , longer than 100 . It must be folded if this file is converted to latex . Because latex doesn ' t care about verbatim lines . The lines are printed as they are .
~ ~ ~
The target type is :
latex
Relative link will be converted when the target type is gfm or html .
Otherwise ( latex ) the link will be removed .
Another relative link .
[ Absolute link ] ( https : / / github . com / ToshioCP ) is kept as it is .
Image link .
If the target type is gfm or html , the size will be removed .
Otherwise ( latex ) it remains .
2022-04-25 03:33:47 +02:00
! [ Screenshot of the box ] ( .. / image / screenshot_lb4 . png ) { width = 6 . 3 cm height = 5 . 325 cm }
2022-04-13 13:33:18 +02:00
EOS
# -------------------
# Return the follwoing array.
# -------------------
{ files : [
[ sample_c , " sample.c " , " C " ] ,
[ sample_h , " sample.h " , " C " ] ,
[ sample_rb , " sample.rb " , " ruby " ] ,
[ rakefile , " Rakefile " , " ruby " ] ,
[ sample_xml , " sample.xml " , " xml " ] ,
[ sample_ui , " sample.ui " , " xml " ] ,
[ sample_y , " sample.y " , " bison " ] ,
[ sample_lex , " sample.lex " , " lex " ] ,
[ sample_build , " sample.build " , " meson " ] ,
[ sample0_md , " sample0.md " , " markdown " ] ,
[ makefile , " Makefile " , " makefile " ] ,
[ sample_src_md , " sample.src.md " , nil ]
] ,
sample_md_gfm : sample_md_gfm ,
sample_md_html : sample_md_html ,
sample_md_latex : sample_md_latex
}
end
2021-02-08 14:24:54 +01:00
end
2022-04-13 13:33:18 +02:00
class Test_lib_src_file < Minitest :: Test
include FileUtils
include Prepare_test
2022-04-16 08:46:29 +02:00
def setup
table_1_src = << ~ EOS
@ @@table
| angle | sine | cosine | tangent |
| - :| :- | :- | :- |
| 0 | #{sin 0}|#{cos 0}|#{tan 0}|
| 1 | #{sin 1*PI/180}|#{cos 1*PI/180}|#{tan 1*PI/180}|
| 2 | #{sin 2*PI/180}|#{cos 2*PI/180}|#{tan 2*PI/180}|
| 3 | #{sin 3*PI/180}|#{cos 3*PI/180}|#{tan 3*PI/180}|
| 4 | #{sin 4*PI/180}|#{cos 4*PI/180}|#{tan 4*PI/180}|
| 5 | #{sin 5*PI/180}|#{cos 5*PI/180}|#{tan 5*PI/180}|
| 6 | #{sin 6*PI/180}|#{cos 6*PI/180}|#{tan 6*PI/180}|
| 7 | #{sin 7*PI/180}|#{cos 7*PI/180}|#{tan 7*PI/180}|
| 8 | #{sin 8*PI/180}|#{cos 8*PI/180}|#{tan 8*PI/180}|
| 9 | #{sin 9*PI/180}|#{cos 9*PI/180}|#{tan 9*PI/180}|
| 10 | #{sin 10*PI/180}|#{cos 10*PI/180}|#{tan 10*PI/180}|
@ @ @
EOS
table_1_dst = << ~ EOS
| angle | sine | cosine | tangent |
| - - - - :| :- - - - - - - - - - - - - - - - - - | :- - - - - - - - - - - - - - - - - | :- - - - - - - - - - - - - - - - - - - |
| 0 | 0 . 0 | 1 . 0 | 0 . 0 |
| 1 | 0 . 0174524064372 8351 | 0 . 9998476951563913 | 0 . 017455064 928217585 |
| 2 | 0 . 034 89949670250097 | 0 . 9993908270190958 | 0 . 034 92076949174773 |
| 3 | 0 . 052335 95624294383 | 0 . 9986295347545738 | 0 . 05240777 9283041196 |
| 4 | 0 . 06 97564737441253 | 0 . 9975640502598242 | 0 . 06 992681194351041 |
| 5 | 0 . 08715574274765817 | 0 . 9961946980917455 | 0 . 08748866352592401 |
| 6 | 0 . 10452846326765346 | 0 . 9945218953682733 | 0 . 10510423526567646 |
| 7 | 0 . 12186934340514748 | 0 . 992546151641322 | 0 . 1227845609029046 |
| 8 | 0 . 13917310096006544 | 0 . 9902680687415704 | 0 . 14054083470239145 |
| 9 | 0 . 15643446504023087 | 0 . 9876883405951378 | 0 . 15838444032453627 |
| 10 | 0 . 17364817766693033 | 0 . 984807753012208 | 0 . 17632698070846498 |
EOS
table_2_src = << ~ 'EOS'
@ @@table
| | token kind | yylval . ID | yylval . NUM |
| :- :| :- | - :| :- :|
| 1 | ID | distance | |
| 2 |= || |
| 3 | NUM || 100 |
| 4 | FD || |
| 5 | ID | distance ||
| 6 | * || |
| 7 | NUM || 2 |
@ @ @
EOS
table_2_dst = << ~ 'EOS'
| | token kind | yylval . ID | yylval . NUM |
| :- :| :- - - - - - - - - | - - - - - - - - :| :- - - - - - - - :|
| 1 | ID | distance | |
| 2 |= | | |
| 3 | NUM | | 100 |
| 4 | FD | | |
| 5 | ID | distance | |
| 6 | * | | |
| 7 | NUM | | 2 |
EOS
@tables = [ [ table_1_src , table_1_dst ] , [ table_2_src , table_2_dst ] ]
# No @@@table or @@@
no_table_1 = << ~ EOS
| angle | sine | cosine | tangent |
| - :| :- | :- | :- |
| 0 | #{sin 0}|#{cos 0}|#{tan 0}|
| 1 | #{sin 1*PI/180}|#{cos 1*PI/180}|#{tan 1*PI/180}|
| 2 | #{sin 2*PI/180}|#{cos 2*PI/180}|#{tan 2*PI/180}|
| 3 | #{sin 3*PI/180}|#{cos 3*PI/180}|#{tan 3*PI/180}|
| 4 | #{sin 4*PI/180}|#{cos 4*PI/180}|#{tan 4*PI/180}|
| 5 | #{sin 5*PI/180}|#{cos 5*PI/180}|#{tan 5*PI/180}|
| 6 | #{sin 6*PI/180}|#{cos 6*PI/180}|#{tan 6*PI/180}|
| 7 | #{sin 7*PI/180}|#{cos 7*PI/180}|#{tan 7*PI/180}|
| 8 | #{sin 8*PI/180}|#{cos 8*PI/180}|#{tan 8*PI/180}|
| 9 | #{sin 9*PI/180}|#{cos 9*PI/180}|#{tan 9*PI/180}|
| 10 | #{sin 10*PI/180}|#{cos 10*PI/180}|#{tan 10*PI/180}|
EOS
# Numbers of the columns are not the same
no_table_2 = << ~ EOS
@ @@table
| angle | sine | cosine | tangent |
| - :| :- | :- | :- |
| 0 | #{sin 0}|#{cos 0}|#{tan 0}|
| 1 | #{sin 1*PI/180}|#{cos 1*PI/180}|#{tan 1*PI/180}|
| 2 | #{sin 2*PI/180}|#{cos 2*PI/180}|#{tan 2*PI/180}|
| 3 | #{sin 3*PI/180}|#{cos 3*PI/180}|#{tan 3*PI/180}|
| 4 | #{sin 4*PI/180}|#{cos 4*PI/180}|
| 5 | #{sin 5*PI/180}|#{cos 5*PI/180}|#{tan 5*PI/180}|
| 6 | #{sin 6*PI/180}|#{cos 6*PI/180}|#{tan 6*PI/180}|
| 7 | #{sin 7*PI/180}|#{cos 7*PI/180}|#{tan 7*PI/180}|
| 8 | #{sin 8*PI/180}|#{cos 8*PI/180}|#{tan 8*PI/180}|
| 9 | #{sin 9*PI/180}|#{cos 9*PI/180}|#{tan 9*PI/180}|
| 10 | #{sin 10*PI/180}|#{cos 10*PI/180}|#{tan 10*PI/180}|
@ @ @
EOS
# Horizontal separator syntax error
no_table_3 = << ~ EOS
@ @@table
| angle | sine | cosine | tangent |
| :: | :- | :- | :- |
| 0 | #{sin 0}|#{cos 0}|#{tan 0}|
| 1 | #{sin 1*PI/180}|#{cos 1*PI/180}|#{tan 1*PI/180}|
| 2 | #{sin 2*PI/180}|#{cos 2*PI/180}|#{tan 2*PI/180}|
| 3 | #{sin 3*PI/180}|#{cos 3*PI/180}|#{tan 3*PI/180}|
| 4 | #{sin 4*PI/180}|#{cos 4*PI/180}|#{tan 4*PI/180}|
| 5 | #{sin 5*PI/180}|#{cos 5*PI/180}|#{tan 5*PI/180}|
| 6 | #{sin 6*PI/180}|#{cos 6*PI/180}|#{tan 6*PI/180}|
| 7 | #{sin 7*PI/180}|#{cos 7*PI/180}|#{tan 7*PI/180}|
| 8 | #{sin 8*PI/180}|#{cos 8*PI/180}|#{tan 8*PI/180}|
| 9 | #{sin 9*PI/180}|#{cos 9*PI/180}|#{tan 9*PI/180}|
| 10 | #{sin 10*PI/180}|#{cos 10*PI/180}|#{tan 10*PI/180}|
@ @ @
EOS
@no_tables = [ no_table_1 , no_table_2 , no_table_3 ]
end
2022-04-13 13:33:18 +02:00
def test_string_partitions
sample = << ~ 'EOS'
For further information , See [ Section 1 ] ( sec1 . src . md ) .
! [ image ] ( .. / image / image . png ) { width = 4 cm , height = 4 cm }
The diagram above shows the relation among groups .
Refer to [ Document ] ( https : / / doc . sampeluniv . ac . jp ) .
EOS
expected = [
" For further information, See " ,
" [Section 1](sec1.src.md) " ,
" . \n " ,
" ![image](../image/image.png){width=4cm, height=4cm} " ,
" \n The diagram above shows the relation among groups. \n Refer to " ,
" [Document](https://doc.sampeluniv.ac.jp) " ,
" . \n "
]
actual = sample . partitions ( / !? \ [.*? \ ] \ (.*? \ )( \ {.*? \ })? / )
assert_equal expected , actual
2021-03-03 15:45:54 +01:00
end
2022-04-16 08:46:29 +02:00
def test_get_alignments
assert_equal [ :c , :l , :r , :l ] , get_alignments ( " |:-:|:---|---:|-| \n " )
end
def test_mkcell
assert_equal " book " , mkcell ( " book " , :l , 9 )
assert_equal " author " , mkcell ( " author " , :r , 8 )
assert_equal " 3600 " , mkcell ( " 3600 " , :c , 6 )
assert_equal " 3600 " , mkcell ( " 3600 " , :c , 7 )
end
def test_mksep
assert_equal " |:--|:------:|--------:|:----:| " , mksep ( [ :l , :c , :r , :c ] , [ 3 , 8 , 9 , 6 ] )
end
def test_at_table
@tables . each do | src , dst |
assert_equal dst , at_table ( src )
end
end
2022-04-13 13:33:18 +02:00
def test_lang
2022-04-16 08:46:29 +02:00
files_src2md ( ) [ :files ] . each do | f |
2022-04-13 13:33:18 +02:00
assert_equal f [ 2 ] , lang ( f [ 1 ] , " gfm " ) if f [ 2 ]
assert_equal f [ 2 ] , lang ( f [ 1 ] , " html " ) if f [ 2 ] && f [ 2 ] =~ / C|ruby|xml|markdown|bison|lex|makefile /
assert_equal f [ 2 ] , lang ( f [ 1 ] , " latex " ) if f [ 2 ] && f [ 2 ] =~ / C|ruby|xml|makefile /
assert_equal " " , lang ( f [ 1 ] , " gfm " ) if f [ 2 ] == nil
2022-04-16 08:46:29 +02:00
assert_equal " " , lang ( f [ 1 ] , " html " ) if f [ 2 ] == nil || ! ( f [ 2 ] =~ / C|ruby|xml|markdown|bison|lex|makefile / )
assert_equal " " , lang ( f [ 1 ] , " latex " ) if f [ 2 ] == nil || ! ( f [ 2 ] =~ / C|ruby|xml|makefile / )
2022-04-13 13:33:18 +02:00
end
2021-02-08 14:24:54 +01:00
end
2022-04-13 13:33:18 +02:00
def test_width
assert_equal [ 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 , 5 , 5 ] , [ 1 , 9 , 10 , 99 , 100 , 999 , 1000 , 9999 , 10000 , 99999 ] . map { | n | width ( n ) }
2021-02-08 14:24:54 +01:00
end
2022-04-13 13:33:18 +02:00
def test_at_if_else
str = << ~ 'EOS'
head
@ @@if gfm
gfm
@ @@elif html
html
@ @@elif latex
latex
@ @@else
others
@ @@end
tail
EOS
assert_equal " head \n gfm \n tail \n " , at_if_else ( str , " gfm " )
assert_equal " head \n html \n tail \n " , at_if_else ( str , " html " )
assert_equal " head \n latex \n tail \n " , at_if_else ( str , " latex " )
assert_equal " head \n others \n tail \n " , at_if_else ( str , " others " )
end
2022-04-16 08:46:29 +02:00
def test_at_include
temp = " temp_at_include " + Time . now . to_f . to_s . gsub ( / \ . / , '' )
2022-04-13 13:33:18 +02:00
Dir . mkdir ( temp ) unless Dir . exist? ( temp )
File . write ( " #{ temp } /temp.c " , << ~ 'EOS' )
#include <stdio.h>
int
fact ( int n ) {
int f ;
if ( n < 0 )
return - 1 ; / * error * /
if ( n == 0 )
return 1 ;
for ( f = 1 ; n > = 1 ; - - n )
f *= n ;
return f ;
}
int
main ( int argc , char ** argv ) {
printf ( " %d \n " , fact ( 5 ) ) ;
}
EOS
expected_c_gfm = << ~ EOS
~ ~ ~ C
1 #include <stdio.h>
2 int
3 fact ( int n ) {
4 int f ;
5 #{" "}
6 if ( n < 0 )
7 return - 1 ; / * error * /
8 if ( n == 0 )
9 return 1 ;
10 for ( f = 1 ; n > = 1 ; - - n )
11 f *= n ;
12 return f ;
13 }
14 #{" "}
15 int
16 main ( int argc , char ** argv ) {
17 printf ( " %d \\ n " , fact ( 5 ) ) ;
18 }
~ ~ ~
EOS
expected_c_pandoc = << ~ 'EOS'
~ ~ ~ { . C . numberLines }
#include <stdio.h>
int
fact ( int n ) {
int f ;
if ( n < 0 )
return - 1 ; / * error * /
if ( n == 0 )
return 1 ;
for ( f = 1 ; n > = 1 ; - - n )
f *= n ;
return f ;
}
int
main ( int argc , char ** argv ) {
printf ( " %d \n " , fact ( 5 ) ) ;
}
~ ~ ~
EOS
File . write ( " #{ temp } /temp.rb " , << ~ 'EOS' )
src = File . read ( " temp.c " )
words = src . split ( / \ W / ) . reject { | s | s == " " } . uniq . sort
print " Words are: \n "
words . each { | word | print " #{ word } \n " }
EOS
expected_ruby_gfm = << ~ 'EOS'
~ ~ ~ ruby
src = File . read ( " temp.c " )
words = src . split ( / \ W / ) . reject { | s | s == " " } . uniq . sort
print " Words are: \n "
words . each { | word | print " #{ word } \n " }
~ ~ ~
EOS
expected_ruby_n_gfm = << ~ 'EOS'
~ ~ ~ ruby
1 src = File . read ( " temp.c " )
2 words = src . split ( / \ W / ) . reject { | s | s == " " } . uniq . sort
3 print " Words are: \n "
4 words . each { | word | print " #{ word } \n " }
~ ~ ~
EOS
expected_ruby_pandoc = << ~ 'EOS'
~ ~ ~ { . ruby }
src = File . read ( " temp.c " )
words = src . split ( / \ W / ) . reject { | s | s == " " } . uniq . sort
print " Words are: \n "
words . each { | word | print " #{ word } \n " }
~ ~ ~
EOS
expected_c_main_gfm = << ~ EOS
~ ~ ~ C
1 int
2 main ( int argc , char ** argv ) {
3 printf ( " %d \\ n " , fact ( 5 ) ) ;
4 }
~ ~ ~
EOS
2022-04-16 08:46:29 +02:00
actual_c_gfm = at_include ( " @@@include -n \n temp.c \n @@@ \n " , temp , " gfm " )
actual_c_pandoc = at_include ( " @@@include \n temp.c \n @@@ \n " , temp , " html " )
actual_ruby_gfm = at_include ( " @@@include -N \n temp.rb \n @@@ \n " , temp , " gfm " )
actual_ruby_pandoc = at_include ( " @@@include -N \n temp.rb \n @@@ \n " , temp , " latex " )
actual_c_ruby_gfm = at_include ( " @@@include -n \n temp.c \n temp.rb \n @@@ \n " , temp , " gfm " )
actual_c_main_gfm = at_include ( " @@@include -n \n temp.c main \n @@@ \n " , temp , " gfm " )
2022-04-13 13:33:18 +02:00
remove_entry_secure ( temp )
# If you want to see the difference
# p Diff::LCS.diff(expected_c_gfm.each_line.to_a, actual_c_gfm.each_line.to_a)
# p Diff::LCS.diff(expected_c_pandoc.each_line.to_a, actual_c_pandoc.each_line.to_a)
assert_equal expected_c_gfm , actual_c_gfm
assert_equal expected_c_pandoc , actual_c_pandoc
assert_equal expected_ruby_gfm , actual_ruby_gfm
assert_equal expected_ruby_pandoc , actual_ruby_pandoc
assert_equal expected_c_gfm + expected_ruby_n_gfm , actual_c_ruby_gfm
assert_equal expected_c_main_gfm , actual_c_main_gfm
end
2022-04-16 08:46:29 +02:00
def test_at_shell
actual = at_shell ( " @@@shell \n echo abc \n @@@ \n " , " . " )
2022-04-13 13:33:18 +02:00
expected = " ~~~ \n $ echo abc \n abc \n ~~~ \n "
assert_equal expected , actual
2021-02-08 14:24:54 +01:00
end
2022-04-13 13:33:18 +02:00
# Change relative links in the secXX.src.md to the one in gfm/secXX.md, html/secXX.html or latex/secXX.tex
# Example:
# src=>gfm: [Section 1](sec1.src.md) => [Section 1](sec1.md)
# src=>html: [Section 1](sec1.src.md) => [Section 1](sec1.html)
# src=>latex: [Section 1](sec1.src.md) => Section 1
# src=>gfm: [document](../doc/document.md) => [document](document.md)
# src=>html: [document](../doc/document.md) => [document]document.html
# src=>latex: [document](../doc/document.md) => document
# src=>latex: [Github](https://github.com/ToshioCP) => [Github](https://github.com/ToshioCP)
# The examples above are simple. But some source files are located in the different directories.
# src/abstract.src.md => ./Readme.md: [Section 1](src/sec1.src.md) => [Section 1](gfm/sec1.md).
# src/abstract.src.md => html/index.md: [Section 1](src/sec1.src.md) => [Section 1](sec1.html).
# src/abstract.src.md => latex/abstract.md: [Section 1](src/sec1.src.md) => Section 1
# src/turtle/turtle_doc.src.md => src/turtle/turtle_doc.md: [Section 1](../sec1.src.md) => [Section 1](../../gfm/sec1.md)
# src/turtle/turtle_doc.src.md => gfm/turtle_doc.md: [Section 1](../sec1.src.md) => [Section 1](sec1.md)
def test_change_link
src_src = << ~ 'EOS'
[ Section 1 ] ( sec1 . src . md )
[ document ] ( .. / doc / document . src . md )
! [ image ] ( .. / image / image . png ) { width = 9 . 0 cm height = 6 . 0 cm }
[ Github ] ( https : / / github . com / ToshioCP )
EOS
src_top = " [Section 1](src/sec1.src.md) \n "
src_turtle = " [Section 1](../sec1.src.md) \n "
dst_src = << ~ 'EOS'
[ Section 1 ] ( sec1 . md )
[ document ] ( document . md )
! [ image ] ( .. / image / image . png )
[ Github ] ( https : / / github . com / ToshioCP )
EOS
assert_equal dst_src , change_link ( src_src , " src " , " gfm " , " gfm " )
dst_src = << ~ 'EOS'
[ Section 1 ] ( sec1 . html )
[ document ] ( document . html )
2022-04-22 05:04:27 +02:00
! [ image ] ( image / image . png )
2022-04-13 13:33:18 +02:00
[ Github ] ( https : / / github . com / ToshioCP )
EOS
assert_equal dst_src , change_link ( src_src , " src " , " html " , " html " )
dst_src = << ~ 'EOS'
Section 1
document
! [ image ] ( .. / image / image . png ) { width = 9 . 0 cm height = 6 . 0 cm }
[ Github ] ( https : / / github . com / ToshioCP )
EOS
assert_equal dst_src , change_link ( src_src , " src " , " latex " , " latex " )
assert_equal " [Section 1](sec1.md) \n " , change_link ( src_top , " . " , " gfm " , " . " )
assert_equal " [Section 1](sec1.html) \n " , change_link ( src_top , " . " , " html " , " html " )
assert_equal " Section 1 \n " , change_link ( src_top , " . " , " latex " , " latex " )
assert_equal " [Section 1](sec1.md) \n " , change_link ( src_turtle , " turtle " , " gfm " , " turtle " )
assert_equal " [Section 1](sec1.md) \n " , change_link ( src_turtle , " turtle " , " gfm " , " gfm " )
assert_equal " [sample.c](../test/temp/sample.c) " , change_link ( " [sample.c](temp/sample.c) " , " test " , " gfm " )
assert_equal " [Section 3](sec3.md) " , change_link ( " [Section 3](sec3.src.md) " , " src " , " gfm " )
assert_equal " [Section 3](sec3.html) " , change_link ( " [Section 3](sec3.src.md) " , " src " , " html " )
assert_equal " Section 3 " , change_link ( " [Section 3](sec3.src.md) " , " src " , " latex " )
2022-04-24 05:13:40 +02:00
src_src = << ~ 'EOS'
[ Section 1 ] ( ` sec1.src.md ` )
[ ` document ` ] ( .. / doc / document . src . md )
` ![image](../image/image.png){width=9.0cm height=6.0cm} ` `
[ Github ` ]( ` https : / / github . com / ToshioCP )
EOS
dst_src = << ~ 'EOS'
[ Section 1 ] ( ` sec1.src.md ` )
[ ` document ` ] ( document . md )
` ![image](../image/image.png){width=9.0cm height=6.0cm} ` `
[ Github ` ]( ` https : / / github . com / ToshioCP )
EOS
assert_equal dst_src , change_link ( src_src , " src " , " gfm " , " gfm " )
2022-04-13 13:33:18 +02:00
end
def test_src2md
2022-04-25 03:33:47 +02:00
cur_dir = pwd ( )
temp = " temp " + Time . now . to_f . to_s . gsub ( / \ . / , '' )
src_dir = " #{ temp } /src "
mkdir_p ( src_dir ) unless Dir . exist? ( src_dir )
2022-04-16 08:46:29 +02:00
files_src2md ( ) [ :files ] . each do | f |
2022-04-25 03:33:47 +02:00
File . write " #{ src_dir } / #{ f [ 1 ] } " , f [ 0 ]
2022-04-13 13:33:18 +02:00
end
2022-04-25 03:33:47 +02:00
File . write ( " #{ src_dir } /sample_image.src.md " , " ![image](image/image.png){width=8cm hight=6cm} \n " )
2022-04-13 13:33:18 +02:00
dst_md = { }
2022-04-25 03:33:47 +02:00
[ " gfm " , " html " , " latex " ] . each do | type |
dst_dir = { " gfm " = > " gfm " , " html " = > " docs " , " latex " = > " latex " } [ type ]
chdir ( temp )
mkdir ( dst_dir ) unless Dir . exist? ( dst_dir )
src2md " src/sample.src.md " , type
src2md " src/sample_image.src.md " , type
dst_md [ type ] = File . read " #{ dst_dir } /sample.md "
dst_md [ " #{ type } _image " ] = File . read " #{ dst_dir } /sample_image.md "
chdir ( cur_dir )
2022-04-13 13:33:18 +02:00
end
remove_entry_secure ( temp )
# If you want to see the difference
2022-04-25 03:33:47 +02:00
# Diff::LCS.diff(files_src2md()[:sample_md_gfm].each_line.to_a, dst_md["gfm"].each_line.to_a).each do |array|
# array.each do |change|
# print "#{change.action} #{change.position.to_s.chomp}: #{change.element.to_s.chomp}\n"
# end
# end
2022-04-13 13:33:18 +02:00
# Diff::LCS.diff(files()[:sample_md_latex].each_line.to_a, dst_md["latex"].each_line.to_a).each do |array|
# array.each do |change|
# print "#{change.action} #{change.position.to_s.chomp}: #{change.element.to_s.chomp}\n"
# end
# end
2022-04-16 08:46:29 +02:00
assert_equal files_src2md ( ) [ :sample_md_gfm ] , dst_md [ " gfm " ]
assert_equal files_src2md ( ) [ :sample_md_html ] , dst_md [ " html " ]
assert_equal files_src2md ( ) [ :sample_md_latex ] , dst_md [ " latex " ]
2022-04-25 03:33:47 +02:00
assert_equal " ![image](../src/image/image.png) \n " , dst_md [ " gfm_image " ]
assert_equal " ![image](image/image.png) \n " , dst_md [ " html_image " ]
assert_equal " ![image](../src/image/image.png){width=8cm hight=6cm} \n " , dst_md [ " latex_image " ]
2022-04-13 13:33:18 +02:00
end
2021-02-08 14:24:54 +01:00
end