From 8bc46b2e3681de6fec77fccf89ede8550471b7bf Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 9 Sep 2015 21:25:09 -0400 Subject: [PATCH] lib/markdown: factorize clones `code_from_block` into `BlockCode::raw_content` Signed-off-by: Jean Privat --- lib/markdown/markdown.nit | 20 ++++++++++++++++++++ src/doc/doc_down.nit | 21 +-------------------- src/testing/testing_doc.nit | 22 +--------------------- 3 files changed, 22 insertions(+), 41 deletions(-) diff --git a/lib/markdown/markdown.nit b/lib/markdown/markdown.nit index 1bb8815..9124ad7 100644 --- a/lib/markdown/markdown.nit +++ b/lib/markdown/markdown.nit @@ -1173,6 +1173,26 @@ abstract class Block block = block.next end end + + # The raw content of the block as a multi-line string. + fun raw_content: String do + var infence = self isa BlockFence + var text = new FlatBuffer + var line = self.block.first_line + while line != null do + if not line.is_empty then + var str = line.value + if not infence and str.has_prefix(" ") then + text.append str.substring(4, str.length - line.trailing) + else + text.append str + end + end + text.append "\n" + line = line.next + end + return text.write_to_string + end end # A block without any markdown specificities. diff --git a/src/doc/doc_down.nit b/src/doc/doc_down.nit index 5284dc1..6482032 100644 --- a/src/doc/doc_down.nit +++ b/src/doc/doc_down.nit @@ -111,7 +111,7 @@ private class NitdocDecorator return end # Try to parse code - var code = code_from_block(block) + var code = block.raw_content var ast = toolcontext.parse_something(code) if ast isa AError then v.add "
"
@@ -150,25 +150,6 @@ private class NitdocDecorator
 		for i in [from..to[ do out.add buffer[i]
 		return out.write_to_string
 	end
-
-	fun code_from_block(block: BlockCode): String do
-		var infence = block isa BlockFence
-		var text = new FlatBuffer
-		var line = block.block.first_line
-		while line != null do
-			if not line.is_empty then
-				var str = line.value
-				if not infence and str.has_prefix("    ") then
-					text.append str.substring(4, str.length - line.trailing)
-				else
-					text.append str
-				end
-			end
-			text.append "\n"
-			line = line.next
-		end
-		return text.write_to_string
-	end
 end
 
 # Decorator for span elements.
diff --git a/src/testing/testing_doc.nit b/src/testing/testing_doc.nit
index 4a4e33c..b5b0f5d 100644
--- a/src/testing/testing_doc.nit
+++ b/src/testing/testing_doc.nit
@@ -290,7 +290,7 @@ private class NitunitDecorator
 	var executor: NitUnitExecutor
 
 	redef fun add_code(v, block) do
-		var code = code_from_block(block)
+		var code = block.raw_content
 		var meta = "nit"
 		if block isa BlockFence and block.meta != null then
 			meta = block.meta.to_s
@@ -321,26 +321,6 @@ private class NitunitDecorator
 		# Add it to the file
 		executor.blocks.last.append code
 	end
-
-	# Extracts code as String from a `BlockCode`.
-	fun code_from_block(block: BlockCode): String do
-		var infence = block isa BlockFence
-		var text = new FlatBuffer
-		var line = block.block.first_line
-		while line != null do
-			if not line.is_empty then
-				var str = line.value
-				if not infence and str.has_prefix("    ") then
-					text.append str.substring(4, str.length - line.trailing)
-				else
-					text.append str
-				end
-			end
-			text.append "\n"
-			line = line.next
-		end
-		return text.write_to_string
-	end
 end
 
 # A unit-test to run
-- 
1.7.9.5