| 118 | } |
| 119 | |
| 120 | CopyrightInfo DocumentContextReader::findCopyright() |
| 121 | { |
| 122 | CopyrightInfo result = {-1, -1, false}; |
| 123 | |
| 124 | QString text = m_document->toPlainText(); |
| 125 | QRegularExpressionMatchIterator matchIterator = getCommentRegex().globalMatch(text); |
| 126 | |
| 127 | QList<CopyrightInfo> copyrightBlocks; |
| 128 | |
| 129 | while (matchIterator.hasNext()) { |
| 130 | QRegularExpressionMatch match = matchIterator.next(); |
| 131 | QString matchedText = match.captured().toLower(); |
| 132 | |
| 133 | bool hasCopyrightIndicator = matchedText.contains("copyright") |
| 134 | || matchedText.contains("(c)") || matchedText.contains("©") |
| 135 | || matchedText.contains("copr.") |
| 136 | || matchedText.contains("all rights reserved") |
| 137 | || matchedText.contains("proprietary") |
| 138 | || matchedText.contains("licensed under") |
| 139 | || matchedText.contains("license:") |
| 140 | || matchedText.contains("gpl") || matchedText.contains("lgpl") |
| 141 | || matchedText.contains("mit license") |
| 142 | || matchedText.contains("apache license") |
| 143 | || matchedText.contains("bsd license") |
| 144 | || matchedText.contains("mozilla public license") |
| 145 | || matchedText.contains("copyleft"); |
| 146 | |
| 147 | bool hasYear = getYearRegex().match(matchedText).hasMatch(); |
| 148 | bool hasName = getNameRegex().match(matchedText).hasMatch(); |
| 149 | |
| 150 | if ((hasCopyrightIndicator && (hasYear || hasName)) || (hasYear && hasName)) { |
| 151 | int startPos = match.capturedStart(); |
| 152 | int endPos = match.capturedEnd(); |
| 153 | |
| 154 | CopyrightInfo info; |
| 155 | info.startLine = m_document->findBlock(startPos).blockNumber(); |
| 156 | info.endLine = m_document->findBlock(endPos).blockNumber(); |
| 157 | info.found = true; |
| 158 | |
| 159 | copyrightBlocks.append(info); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | for (int i = 0; i < copyrightBlocks.size() - 1; ++i) { |
| 164 | if (copyrightBlocks[i].endLine + 1 >= copyrightBlocks[i + 1].startLine) { |
| 165 | copyrightBlocks[i].endLine = copyrightBlocks[i + 1].endLine; |
| 166 | copyrightBlocks.removeAt(i + 1); |
| 167 | --i; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (!copyrightBlocks.isEmpty()) { // temproary solution, need cache |
| 172 | return copyrightBlocks.first(); |
| 173 | } |
| 174 | |
| 175 | return result; |
| 176 | } |
| 177 | |