Skip to content

Commit c000008

Browse files
authored
Merge pull request #42 from CourseOrchestra/fit_images_without_width
Added fit width for images without width
2 parents 3405991 + 030b05c commit c000008

File tree

7 files changed

+66
-26
lines changed

7 files changed

+66
-26
lines changed

lib/a-od-producer/asciidoctor-od.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
$aodp_sn_index_2 = "Index_20_2"
107107
$aodp_sn_index_3 = "Index_20_3"
108108
$aodp_sn_alphabetical_index_pagenum = "Alphabetical_20_Index_20_Pagenum"
109+
$aodp_assume_src_dpi = 130
110+
$aodp_list_first_level_indent = "12.7mm"
111+
$aodp_list_other_levels_indent = "6.4mm"
109112

110113
=begin
111114
tag::algorithm_description[]
@@ -649,6 +652,8 @@ def h_svg_width_height
649652
img_ew = @snr.match(re)[1]
650653
end
651654

655+
656+
652657
re = / ip_t_([a-z]+) /
653658
type = @snr.match(re)[1]
654659

@@ -665,14 +670,32 @@ def h_svg_width_height
665670
end
666671

667672
re = / ip_sd_([0-9]+) /
668-
srcdpi = 100 # assume image is 100dpi
673+
srcdpi = $aodp_assume_src_dpi
669674
srcdpi = @snr.match(re)[1].to_f if !!(@snr =~ re)
670675

671676
re = / ip_su_([a-z]+) /
672677
svgunit = 'px'
673678
svgunit = @snr.match(re)[1] if !!(@snr =~ re)
674679

675-
if !!(@snr !~ / ip_fr_/)
680+
# if guessing then use fictrect strategy with a 100% square minus list indent
681+
# todo should count 100% from parent inner width
682+
guess_width = false
683+
if !!(@snr !~ / ip_fr_/) && !!(@snr !~ / ip_w_[0-9]/)
684+
list_level = 0
685+
re = / in_list_item_([0-9]) /
686+
list_level = @snr.match(re)[1].to_f if !!(@snr =~ re)
687+
list_first_level_indent_mm = MiscMethods.text_measurement_to_mm $aodp_list_first_level_indent
688+
list_other_levels_indent_mm = MiscMethods.text_measurement_to_mm $aodp_list_other_levels_indent
689+
rect_w = $aodp_100_percent_mm
690+
rect_h = $aodp_100_percent_mm
691+
if list_level > 0
692+
rect_w = rect_w - list_first_level_indent_mm - (list_level - 1) * list_other_levels_indent_mm
693+
end
694+
guess_width = true
695+
end
696+
697+
698+
if !!(@snr !~ / ip_fr_/) && !guess_width
676699
re = /([0-9\.]+)([a-z_]+)/
677700
img_cw = img_ew.match(re)[1].to_f
678701
img_cunit = img_ew.match(re)[2]

lib/a-od-producer/misc.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
class MiscMethods
2+
def self.text_measurement_to_mm text_measurement
3+
re = /^([0-9]+[\.]?[0-9]*)([a-z]+)$/
4+
if !!(text_measurement =~ re)
5+
measurement = text_measurement.match(re)[1].to_f
6+
unit = text_measurement.match(re)[2]
7+
else
8+
raise "\"#{text_measurement}\" is not a text measurement value"
9+
end
10+
measurement_to_mm measurement, unit
11+
end
212
def self.measurement_to_mm measurement, unit
313
coef = {mm: 1.0,
414
cm: 10.0,

lib/slim/helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def set_current_cell_style op, cell_style = ""
1111

1212
def set_current_list_item_style op, list_item_style = ""
1313
$aod_ll += op
14-
$aod_current_list_item_style[$aod_ll] = "in_list_item_#{$aod_tl} #{list_item_style}" if $aod_ll > 0
14+
$aod_current_list_item_style[$aod_ll] = "in_list_item_#{$aod_ll} #{list_item_style}" if $aod_ll > 0
1515
end
1616

1717
# Calculates style based on role and position in some elements

test/test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
require_relative "../lib/a-od-producer/misc.rb"
33

44
class TestMiscMethods < Minitest::Test
5+
describe "Method text_measurement_to_mm " do
6+
it "should correctly calculate measurement in mm" do
7+
assert_equal MiscMethods.text_measurement_to_mm("1pt").round(3), 0.353
8+
assert_equal MiscMethods.text_measurement_to_mm("1.5pt").round(3), 0.529
9+
assert_raises RuntimeError do
10+
MiscMethods.measurement_to_mm(1, "inches")
11+
end
12+
end
13+
end
514
describe "Method measurement_to_mm " do
615
it "should correctly calculate measurement in mm" do
716
assert_equal MiscMethods.measurement_to_mm(1, "mm").round(3), 1.000

test/test_cases/stew/long_line.png

617 Bytes
Loading
1.33 KB
Loading

test/test_cases/stew/test.adoc

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
1414
{nbsp}
1515
16-
|===
17-
l| cat foo.txt \| grep bar
18-
|===
19-
20-
2116
Some sort of [((preamble))]
2217
multiline paragraph.
2318
@@ -47,7 +42,7 @@ https://asciidoctor.org/[Asciidoctor]
4742
== Diagram extension
4843
4944
.A diagram in adoc file
50-
[plantuml, diagram-classes, png, fitrect="170x225mm", srcdpi=300, width = "70%"]
45+
[plantuml, diagram-classes, png, fitrect="170x225mm", srcdpi=300, width = "70%"]
5146
....
5247
skinparam dpi 300
5348
@@ -79,7 +74,7 @@ class BasicParagraph < BasicHelper <1> <2>
7974
def h_parent_style_name <2>
8075
@sd[:parent_style_name] = $defsn_p
8176
a = b + <3>
82-
abc[1]
77+
abc[1]
8378
end
8479
end
8580
----
@@ -136,7 +131,7 @@ a|
136131
137132
[cols="1,2"]
138133
!===
139-
!Just for the fun of it
134+
!Just for the fun of it
140135
a!
141136
.Some header
142137
[example]
@@ -149,9 +144,9 @@ A deeply digged example. Interesting to see the number
149144

150145
Some text.
151146

152-
[IMPORTANT]
147+
[IMPORTANT]
153148
.Feeding the Werewolves
154-
====
149+
====
155150
While werewolves are hardy community members, keep in mind the following dietary concerns:
156151
157152
. They are allergic to cinnamon.
@@ -178,7 +173,7 @@ C = \alpha + \beta Y^{\gamma} + \epsilon
178173
.({counter: eq})
179174
++++
180175
\begin{bmatrix}
181-
a & b \\
176+
a & b \\
182177
c & d
183178
\end{bmatrix}\binom{n}{k}
184179
++++
@@ -240,9 +235,15 @@ image::ruler.png[fitrect="170x225mm", srcdpi=100, width = "10%"]
240235

241236
[square]
242237
.Marked list heading
243-
* First point
244-
** Subpoint 1
245-
. And a little mix-in
238+
* First point (long line with fitted width)
239+
+
240+
image::long_line.png[]
241+
** Subpoint 1 (long line with fitted width)
242+
+
243+
image::long_line.png[]
244+
. And a little mix-in (short line that shouldn't be fitted due to size)
245+
+
246+
image::short_line.png[]
246247
** Subpoint 2
247248
* Second point with no bullet
248249
* Third point
@@ -294,7 +295,7 @@ Example of informal table with no vertical lines.
294295
[.portrait]
295296
=== A Test Section 1.1 (portrait)
296297

297-
A Table (<<t1>>) Test paragraph text. A Test paragraph text. A Test paragraph text. A Test paragraph text. A Test paragraph text. A Test paragraph text. A Test paragraph text.
298+
A Table (<<t1>>) Test paragraph text. A Test paragraph text. A Test paragraph text. A Test paragraph text. A Test paragraph text. A Test paragraph text. A Test paragraph text.
298299

299300
[.text-align-center]
300301
A Test centered paragraph text. A Test centered paragraph text. A Test centered paragraph text. A Test centered paragraph text. A Test centered paragraph text. A Test centered paragraph text. A Test centered paragraph text. A Test centered paragraph text. A Test centered paragraph text.
@@ -307,16 +308,16 @@ A Test centered paragraph text. A Test centered paragraph text. A Test centered
307308

308309
.^|b1 centered vertically
309310
|c1 Here a very long multiline text is situated in order to show that vertical alignment works
310-
2+a|a2 spanned
311+
2+a|a2 spanned
311312

312-
Asciidoc contents
313+
Asciidoc contents
313314

314315
* a
315316
. a1
316317
. a2
317318
+
318319
And some text as a paragraph to check paragraph in list alignment
319-
* b
320+
* b
320321

321322
|c2
322323
h|a3 (cell header)
@@ -351,10 +352,10 @@ And some TIP in a table
351352
3+|a9
352353
3+|a10
353354

354-
|===
355+
|===
355356

356357
[.text-align-right]
357-
A Test right-aligned paragraph text.
358+
A Test right-aligned paragraph text.
358359

359360
== Some long, very long, very very long, very very very long, very very very very long h e a d i n g of level 1 (doesn't hang over page number)
360361
=== Some long, very long, very very long, very very very long, very very very very long h e a d i n g of level 2
@@ -367,6 +368,3 @@ A Test right-aligned paragraph text.
367368
== Appendix example
368369

369370
An appendix
370-
371-
[index]
372-
== An index

0 commit comments

Comments
 (0)