Serenity Operating System
at portability 80 lines 2.7 kB view raw
1/* 2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/StringBuilder.h> 28#include <LibMarkdown/MDHeading.h> 29 30String MDHeading::render_to_html() const 31{ 32 StringBuilder builder; 33 builder.appendf("<h%d>", m_level); 34 builder.append(m_text.render_to_html()); 35 builder.appendf("</h%d>\n", m_level); 36 return builder.build(); 37} 38 39String MDHeading::render_for_terminal() const 40{ 41 StringBuilder builder; 42 43 switch (m_level) { 44 case 1: 45 case 2: 46 builder.append("\n\033[1m"); 47 builder.append(m_text.render_for_terminal().to_uppercase()); 48 builder.append("\033[0m\n"); 49 break; 50 default: 51 builder.append("\n\033[1m"); 52 builder.append(m_text.render_for_terminal()); 53 builder.append("\033[0m\n"); 54 break; 55 } 56 57 return builder.build(); 58} 59 60bool MDHeading::parse(Vector<StringView>::ConstIterator& lines) 61{ 62 if (lines.is_end()) 63 return false; 64 65 const StringView& line = *lines; 66 67 for (m_level = 0; m_level < (int)line.length(); m_level++) 68 if (line[(size_t)m_level] != '#') 69 break; 70 71 if (m_level >= (int)line.length() || line[(size_t)m_level] != ' ') 72 return false; 73 74 StringView title_view = line.substring_view((size_t)m_level + 1, line.length() - (size_t)m_level - 1); 75 bool success = m_text.parse(title_view); 76 ASSERT(success); 77 78 ++lines; 79 return true; 80}