Skip to content

Commit 96f8f7c

Browse files
committed
feat(string_t): build markdown table from array
This commit adds support for, and an example demonstrating, function invocations of the form integer, parameter :: array(*,*) = reshape([1,2,3,4], [2,2]) markdown_table(string_t(array), side_borders=.true.) where the function result contains an 1D string_t array in which each element represents one row of a Markdown table created from one row of the 2D integer_array. The above approach leverages the elemental attribute of the string_t constructor function and therefore also supports real, double precision, and complex values. The approach also works for string_t arrays, in which case there is no need to invoke the string_t constructor.
1 parent bd10da6 commit 96f8f7c

4 files changed

Lines changed: 61 additions & 5 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
program build_markdown_table_from_array
2+
!! Build a GitHub Markdown table from an array
3+
use julienne_string_m, only : string_t, markdown_table
4+
implicit none
5+
6+
integer row, col
7+
integer, parameter :: rows = 3, cols = 4
8+
integer, parameter :: integer_array(*,*) = reshape([([(row*col, row=1,rows)], col=1,cols)], [rows,cols])
9+
real, parameter :: real_array(*,*) = reshape([([(real(row*col), row=1,rows)], col=1,cols)], [rows,cols])
10+
type(string_t), allocatable :: table_lines(:)
11+
12+
table_lines = markdown_table(string_t(integer_array), side_borders=.true.)
13+
14+
do row = 1, size(table_lines)
15+
print '(a)', table_lines(row)%string()
16+
end do
17+
18+
table_lines = markdown_table(string_t(real_array), side_borders=.false.)
19+
20+
do row = 1, size(table_lines)
21+
print '(a)', table_lines(row)%string()
22+
end do
23+
24+
end program

src/julienne/julienne_string_m.f90

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ module julienne_string_m
77

88
private
99
public :: string_t
10-
public :: array_of_strings
11-
public :: operator(.cat.) ! element-wise concatenation unary operator
12-
public :: operator(.csv.) ! comma-separated values unary operator
13-
public :: operator(.sv.) ! separated-values binary operator
10+
public :: array_of_strings ! construct 1D string_t array from a string containing delimited substrings
11+
public :: markdown_table ! construct 1D string_t array in which each element is a GitHub Markdown table row
12+
public :: operator(.cat.) ! element-wise concatenation unary operator
13+
public :: operator(.csv.) ! comma-separated values unary operator
14+
public :: operator(.sv.) ! separated-values binary operator
1415

1516
type, extends(characterizable_t) :: string_t
1617
private
@@ -163,6 +164,17 @@ pure module function strings_with_string_t_separator(strings, separator) result(
163164

164165
end interface
165166

167+
interface markdown_table
168+
169+
pure module function markdown_table_from_strings(array, side_borders) result(lines)
170+
implicit none
171+
type(string_t), intent(in) :: array(:,:)
172+
logical, intent(in) :: side_borders
173+
type(string_t) lines(size(array,1))
174+
end function
175+
176+
end interface
177+
166178
interface
167179

168180
pure module function as_character(self) result(raw_string)

src/julienne/julienne_string_s.F90

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@
112112

113113
end procedure
114114

115+
module procedure markdown_table_from_strings
116+
117+
integer row
118+
character(len=1), parameter :: column_separator = "|"
119+
120+
associate(num_rows => size(array,1))
121+
!allocate(lines(num_rows))
122+
do row = 1, num_rows
123+
associate(line => array(row,:) .sv. column_separator)
124+
if (side_borders) then
125+
lines(row) = line%bracket(column_separator)
126+
else
127+
lines(row) = line
128+
end if
129+
end associate
130+
end do
131+
end associate
132+
133+
end procedure
134+
115135
module procedure array_of_strings
116136
character(len=:), allocatable :: remainder, next_string
117137
integer next_delimiter, string_end

src/julienne_m.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module julienne_m
77
use julienne_file_m, only : file_t
88
use julienne_github_ci_m, only : github_ci
99
use julienne_formats_m, only : separated_values, csv
10-
use julienne_string_m, only : string_t, operator(.cat.), operator(.csv.), operator(.sv.)
10+
use julienne_string_m, only : string_t, operator(.cat.), operator(.csv.), operator(.sv.), array_of_strings, markdown_table
1111
use julienne_test_m, only : test_t, test_description_substring
1212
use julienne_test_description_m, only : test_description_t, diagnosis_function_i
1313
use julienne_test_diagnosis_m, only : test_diagnosis_t

0 commit comments

Comments
 (0)