Skip to content

Commit

Permalink
Added some more html tags and option to remove them from paragraph.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiman-al-masoud committed Aug 8, 2021
1 parent 7038f63 commit 24dec2b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,27 @@ private int getLine(int pos){
}


/**
* Get the html source as a list of paragraphs.
* @return
*/
private String[] getParagraphs(){
//split the html source by end of paragraph tags
String[] pars = getText().split("</p>");

//adjust each paragraph
for(int i =0; i<pars.length; i++){
pars[i] = pars[i].replaceAll("\n", "");
pars[i] = pars[i]+" </p>";
}

return pars;
}


/**
* Surround some text with an html tag and save.
* (Works on entire paragraphs.)
* @param pos
* @param tag
*/
Expand All @@ -459,14 +477,8 @@ public void addHtmlTag(int pos, String tag){
int lineNum = getLine(pos);
Log.d("LINE_NUM", lineNum+"");

//split the html source by end of paragraph tags
String[] pars = getText().split("</p>");

//adjust each paragraph
for(int i =0; i<pars.length; i++){
pars[i] = pars[i].replaceAll("\n", "");
pars[i] = pars[i]+" </p>";
}
//get the paragraphs
String[] pars = getParagraphs();

//Log.d("LINE_NUM", "PARAGRAPHS:");
//for(int i =0; i<pars.length-1; i++){
Expand Down Expand Up @@ -498,6 +510,40 @@ public void addHtmlTag(int pos, String tag){

}

/**
* Remove all html tags from a paragraph.
* @param pos
*/
@Override
public void removeHtmlTags(int pos){

int lineNum = getLine(pos);

//get the paragraphs
String[] pars = getParagraphs();

//convert the line number to the paragraph number.
lineNum = (int)(((double)0.5*lineNum) -0.5);

String modifiedPar = pars[lineNum];

//remove all tags other than the paragraph tag. (sort of)
modifiedPar = modifiedPar.replaceAll("<[abcefghijklmnoqrstuvwxyz]>", "").replaceAll("</[abcefghijklmnoqrstuvwxyz]>", "");

//replace the paragraph
pars[lineNum] = modifiedPar;

//re-build the html source from the single paragraphs.
String newHtml = "";
for(String par : pars){
newHtml+=par;
}

//save it.
setText(newHtml);

}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public interface Page extends Serializable {
public void addHtmlTag(int pos, String tag);


/**
* Remove all of the html tags from a position.
* @param pos
*/
public void removeHtmlTags(int pos);



}
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,19 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Toast.makeText(this, currentMode, Toast.LENGTH_LONG).show();
break;
case R.id.make_bold:
applyTag("b");
break;
case R.id.make_underlined:
applyTag("u");
break;
case R.id.make_italics:
applyTag("i");
break;

case R.id.make_plain:
int currentPos = textView.getSelectionStart();
saveToPage();
page.addHtmlTag(textView.getSelectionStart(), "b");
page.removeHtmlTags(textView.getSelectionStart());
reloadText();
jumpToPosition(currentPos);
break;
Expand All @@ -291,6 +301,17 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
}


@RequiresApi(api = Build.VERSION_CODES.N)
private void applyTag(String tag){
int currentPos = textView.getSelectionStart();
saveToPage();
page.addHtmlTag(textView.getSelectionStart(), tag);
reloadText();
jumpToPosition(currentPos);
}



/**
* Uses volume keys to navigate up and down between token positions.
* @param keyCode
Expand Down
32 changes: 30 additions & 2 deletions app/src/main/res/menu/reader_activity_toolbar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,37 @@
android:icon="@android:drawable/btn_plus"
android:title="enlarge text"
app:showAsAction="always" />



<item
android:id="@+id/make_bold"
android:title="bold" />
android:id="@+id/styles_menu"
android:title="style">

<menu>


<item
android:id="@+id/make_plain"
android:title="plain" />

<item
android:id="@+id/make_bold"
android:title="bold" />

<item
android:id="@+id/make_underlined"
android:title="underlined"/>

<item
android:id="@+id/make_italics"
android:title="italics"/>



</menu>

</item>



Expand Down

0 comments on commit 24dec2b

Please sign in to comment.