/src/libreoffice/comphelper/source/misc/markdown.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | */ |
9 | | |
10 | | #include <comphelper/markdown.hxx> |
11 | | |
12 | | #include <unicode/regex.h> |
13 | | |
14 | | constexpr sal_Int32 MAX_SEARCH_CHARS = 2000; |
15 | | |
16 | | namespace comphelper |
17 | | { |
18 | | bool IsMarkdownData(OUString& rData) |
19 | 0 | { |
20 | 0 | icu::UnicodeString ustr(rData.getStr(), std::min(rData.getLength(), MAX_SEARCH_CHARS)); |
21 | |
|
22 | 0 | UErrorCode status = U_ZERO_ERROR; |
23 | |
|
24 | 0 | static std::unique_ptr<icu::RegexPattern> patternHeadings( |
25 | 0 | icu::RegexPattern::compile(uR"(^\s*#{1,6}\s+.*)", UREGEX_MULTILINE, status)); |
26 | |
|
27 | 0 | static std::unique_ptr<icu::RegexPattern> patternLinks( |
28 | 0 | icu::RegexPattern::compile(uR"(\[.*?\]\(.*?\))", 0, status)); |
29 | |
|
30 | 0 | static std::unique_ptr<icu::RegexPattern> patternCode( |
31 | 0 | icu::RegexPattern::compile(uR"(```[\s\S]*?```)", 0, status)); |
32 | |
|
33 | 0 | static std::unique_ptr<icu::RegexPattern> patternItalic( |
34 | 0 | icu::RegexPattern::compile(uR"(([*_])(?!\s)(.+?)(?<!\s)\1)", 0, status)); |
35 | |
|
36 | 0 | static std::unique_ptr<icu::RegexPattern> patternBold( |
37 | 0 | icu::RegexPattern::compile(uR"(([*_])\1(?!\s)(.+?)(?<!\s)\1\1)", 0, status)); |
38 | |
|
39 | 0 | static std::unique_ptr<icu::RegexPattern> patternStrikethrough( |
40 | 0 | icu::RegexPattern::compile(uR"(~~(?!\s)(.+?)(?<!\s)~~)", 0, status)); |
41 | |
|
42 | 0 | static std::unique_ptr<icu::RegexPattern> patternHorizontalRule( |
43 | 0 | icu::RegexPattern::compile(uR"(^(?:-{3,}|\*{3,}|_{3,})$)", UREGEX_MULTILINE, status)); |
44 | |
|
45 | 0 | static std::unique_ptr<icu::RegexPattern> patternBlockQuotes( |
46 | 0 | icu::RegexPattern::compile(uR"(^\s*>+.*$)", UREGEX_MULTILINE, status)); |
47 | |
|
48 | 0 | static std::unique_ptr<icu::RegexPattern> patternTables(icu::RegexPattern::compile( |
49 | 0 | uR"(^\s*\|?(?:[^|\n]+?\|)+[^|\n]*\|?\s*$)", UREGEX_MULTILINE, status)); |
50 | |
|
51 | 0 | if (U_FAILURE(status)) |
52 | 0 | return false; |
53 | | |
54 | 0 | return patternHeadings->matcher(ustr, status)->find() |
55 | 0 | || patternLinks->matcher(ustr, status)->find() |
56 | 0 | || patternCode->matcher(ustr, status)->find() |
57 | 0 | || patternItalic->matcher(ustr, status)->find() |
58 | 0 | || patternBold->matcher(ustr, status)->find() |
59 | 0 | || patternStrikethrough->matcher(ustr, status)->find() |
60 | 0 | || patternHorizontalRule->matcher(ustr, status)->find() |
61 | 0 | || patternBlockQuotes->matcher(ustr, status)->find() |
62 | 0 | || patternTables->matcher(ustr, status)->find(); |
63 | 0 | } |
64 | | } |
65 | | |
66 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |