@ -53,6 +53,7 @@ public class CommentParser {
private Pattern colorPattern = Pattern . compile ( "color:#([0-9a-fA-F]+)" ) ;
private Pattern colorPattern = Pattern . compile ( "color:#([0-9a-fA-F]+)" ) ;
private Map < String , List < StyleRule > > rules = new HashMap < > ( ) ;
private Map < String , List < StyleRule > > rules = new HashMap < > ( ) ;
private List < String > internalDomains = new ArrayList < > ( 0 ) ;
public CommentParser ( ) {
public CommentParser ( ) {
// Required tags.
// Required tags.
@ -101,6 +102,10 @@ public class CommentParser {
this . fullQuotePattern = fullQuotePattern ;
this . fullQuotePattern = fullQuotePattern ;
}
}
public void addInternalDomain ( String domain ) {
this . internalDomains . add ( domain ) ;
}
public CharSequence handleTag ( PostParser . Callback callback ,
public CharSequence handleTag ( PostParser . Callback callback ,
Theme theme ,
Theme theme ,
Post . Builder post ,
Post . Builder post ,
@ -233,11 +238,29 @@ public class CommentParser {
public Link matchAnchor ( Post . Builder post , CharSequence text , Element anchor , PostParser . Callback callback ) {
public Link matchAnchor ( Post . Builder post , CharSequence text , Element anchor , PostParser . Callback callback ) {
String href = anchor . attr ( "href" ) ;
String href = anchor . attr ( "href" ) ;
// For inner links we handle it as relative (for sites that have multiple domains).
String path = "" ;
if ( href . startsWith ( "//" ) | | href . startsWith ( "http://" ) | | href . startsWith ( "https://" ) ) {
int offset = href . startsWith ( "//" ) ? 2 : ( href . startsWith ( "http://" ) ? 7 : 8 ) ;
String domain = href . substring ( Math . min ( href . length ( ) , offset ) ,
Math . min ( href . length ( ) , href . indexOf ( '/' , offset ) ) ) ;
// Whitelisting domains is optional.
// If you don't specify it it will purely use the quote patterns to match.
if ( internalDomains . isEmpty ( ) | | internalDomains . contains ( domain ) ) {
int pathStart = href . indexOf ( '/' , offset ) ;
if ( pathStart > = 0 ) {
path = href . substring ( pathStart ) ;
}
}
} else {
path = href ;
}
PostLinkable . Type t ;
PostLinkable . Type t ;
Object value ;
Object value ;
Matcher externalMatcher = fullQuotePattern . matcher ( href ) ;
Matcher externalMatcher = fullQuotePattern . matcher ( pat h) ;
if ( externalMatcher . matches ( ) ) {
if ( externalMatcher . matches ( ) ) {
String board = externalMatcher . group ( 1 ) ;
String board = externalMatcher . group ( 1 ) ;
int threadId = Integer . parseInt ( externalMatcher . group ( 2 ) ) ;
int threadId = Integer . parseInt ( externalMatcher . group ( 2 ) ) ;
@ -251,12 +274,12 @@ public class CommentParser {
value = new PostLinkable . ThreadLink ( board , threadId , postId ) ;
value = new PostLinkable . ThreadLink ( board , threadId , postId ) ;
}
}
} else {
} else {
Matcher quoteMatcher = quotePattern . matcher ( href ) ;
Matcher quoteMatcher = quotePattern . matcher ( pat h) ;
if ( quoteMatcher . matches ( ) ) {
if ( quoteMatcher . matches ( ) ) {
t = PostLinkable . Type . QUOTE ;
t = PostLinkable . Type . QUOTE ;
value = Integer . parseInt ( quoteMatcher . group ( 1 ) ) ;
value = Integer . parseInt ( quoteMatcher . group ( 1 ) ) ;
} else {
} else {
// normal link
// normal link, use original href
t = PostLinkable . Type . LINK ;
t = PostLinkable . Type . LINK ;
value = href ;
value = href ;
}
}