]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/winterwell.markdown/src/winterwell/markdown/editors/HeaderWithUnderlineRule.java
Tycho compilation changes for SVN version also.
[simantics/platform.git] / bundles / winterwell.markdown / src / winterwell / markdown / editors / HeaderWithUnderlineRule.java
diff --git a/bundles/winterwell.markdown/src/winterwell/markdown/editors/HeaderWithUnderlineRule.java b/bundles/winterwell.markdown/src/winterwell/markdown/editors/HeaderWithUnderlineRule.java
new file mode 100644 (file)
index 0000000..f5970d0
--- /dev/null
@@ -0,0 +1,73 @@
+/**\r
+ * @author Telmo Brugnara\r
+ * 10 Feb 2014\r
+ */\r
+package winterwell.markdown.editors;\r
+\r
+import org.eclipse.jface.text.rules.ICharacterScanner;\r
+import org.eclipse.jface.text.rules.IRule;\r
+import org.eclipse.jface.text.rules.IToken;\r
+import org.eclipse.jface.text.rules.Token;\r
+\r
+public class HeaderWithUnderlineRule implements IRule {\r
+\r
+       IToken successToken = null;\r
+       \r
+       public HeaderWithUnderlineRule(IToken token) {\r
+               successToken = token;\r
+       }\r
+       \r
+       public IToken evaluate(ICharacterScanner scanner) {\r
+               int c = -1;\r
+               int scanCount = 0;\r
+               if (scanner.getColumn()==0) {\r
+                       do {\r
+                               c = scanner.read();\r
+                               scanCount++;\r
+                       } while (!isNewLine((char) c) && c != ICharacterScanner.EOF);\r
+                       if(c == ICharacterScanner.EOF) {\r
+                               // is not a header\r
+                               for(int i=0;i<scanCount;i++) { scanner.unread(); }\r
+                               return Token.UNDEFINED;\r
+                       }\r
+                       c = scanner.read();\r
+                       scanCount++;\r
+                       if(c == '\r') {\r
+                               c = scanner.read();\r
+                               scanCount++;\r
+                       }\r
+                       if(!isUnderline((char) c)) {\r
+                               // is not a header\r
+                               for(int i=0;i<scanCount;i++) { scanner.unread(); }\r
+                               return Token.UNDEFINED;\r
+                       }\r
+                       do {\r
+                               c = scanner.read();\r
+                               scanCount++;\r
+                               if(isNewLine((char) c) || c == ICharacterScanner.EOF) {\r
+                                       //scanner.unread();\r
+                                       return successToken;\r
+                               }\r
+                               if(!isUnderline((char) c) && !isWhitespace((char) c) && c != '\r') {\r
+                                       // is not a header\r
+                                       for(int i=0;i<scanCount;i++) { scanner.unread(); }\r
+                                       return Token.UNDEFINED;\r
+                               }\r
+                       } while (true);\r
+               }\r
+\r
+               return Token.UNDEFINED;\r
+       }\r
+       \r
+       boolean isNewLine(char c) {\r
+               return c == '\n';\r
+       }\r
+\r
+       boolean isUnderline(char c) {\r
+               return c == '=' || c == '-';\r
+       }\r
+\r
+       boolean isWhitespace(char c) {\r
+               return c == ' ' || c == '\t';\r
+       }\r
+}\r