mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 20:12:43 +00:00 
			
		
		
		
	 dee84113ab
			
		
	
	
		dee84113ab
		
	
	
	
	
		
			
			This patch changes the HTML formatting (where to put newlines, etc...) to better match commonmark's test cases. This has minimal effect of the correctness of our markdown implementation, but makes it easier to test. Changes: - Use <em> instead of <i>. - Newline before end of code block. - <hr /> instead of <hr>. - Newline before first list item. - Newline between lines of a paragraph. - Trim whitespace on lines of paragraphs. Tests passed: 33/652 -> 87/652
		
			
				
	
	
		
			52 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <AK/StringBuilder.h>
 | |
| #include <LibMarkdown/Paragraph.h>
 | |
| 
 | |
| namespace Markdown {
 | |
| 
 | |
| String Paragraph::render_to_html() const
 | |
| {
 | |
|     StringBuilder builder;
 | |
|     builder.append("<p>");
 | |
|     bool first = true;
 | |
|     for (auto& line : m_lines) {
 | |
|         if (!first)
 | |
|             builder.append('\n');
 | |
|         first = false;
 | |
|         builder.append(line.text().render_to_html().trim(" \t"));
 | |
|     }
 | |
|     builder.append("</p>\n");
 | |
|     return builder.build();
 | |
| }
 | |
| 
 | |
| String Paragraph::render_for_terminal(size_t) const
 | |
| {
 | |
|     StringBuilder builder;
 | |
|     bool first = true;
 | |
|     for (auto& line : m_lines) {
 | |
|         if (!first)
 | |
|             builder.append(' ');
 | |
|         first = false;
 | |
|         builder.append(line.text().render_for_terminal());
 | |
|     }
 | |
|     builder.append("\n\n");
 | |
|     return builder.build();
 | |
| }
 | |
| 
 | |
| OwnPtr<Paragraph::Line> Paragraph::Line::parse(Vector<StringView>::ConstIterator& lines)
 | |
| {
 | |
|     if (lines.is_end())
 | |
|         return {};
 | |
| 
 | |
|     auto text = Text::parse(*lines++);
 | |
|     if (!text.has_value())
 | |
|         return {};
 | |
| 
 | |
|     return make<Paragraph::Line>(text.release_value());
 | |
| }
 | |
| }
 |